参考同上一个贴:
纯于练手。
#HTTPserver import socket HOST = '' PORT = 8088 text_content = '''HTTP/1.x 200 OK Content-Type: text/html <html> <head> <title>WOW</title> </head> <body> <p>WOW, Python Server</p> </body> </html> ''' f = open('hello.jpg', 'rb') pic_content = ''' HTTP/1.1 200 OK Content-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()
时间: 2024-10-29 23:08:18