参考同上一个贴:
纯于练手。
#HTTPserverimport socketHOST = ''PORT = 8088text_content = '''HTTP/1.x 200 OKContent-Type: text/htmlWOW WOW, Python Server
'''f = open('hello.jpg', 'rb')pic_content = '''HTTP/1.1 200 OKContent-Type: image/jpeg'''pic_content = pic_content + f.read()f.close()s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)s.bind((HOST, PORT))print 'HTTPserver is start listen...'while True: s.listen(3) conn, addr = s.accept() request = conn.recv(1024) print request.split(' ') method = request.split(' ')[0] src = request.split(' ')[1] if method == 'GET': if src == '/hello.jpg': content = pic_content else: content = text_content print 'Connetcted by', addr print 'Request is:', request conn.sendall(content) conn.close()