#! /usr/bin/python2 # coding=utf-8 # Written by Vamei import socket import time # Address HOST = '127.0.0.1' PORT = 8888 def read_file(file_name): print "---------------------------",file_name f = open('test.jpg', 'rb') pic_content = '''\nHTTP/1.x 200 OK\nContent-Type: image/gif\n\n''' pic_content = pic_content + f.read() f.close() return pic_content # Prepare HTTP response text_content = '''HTTP/1.x 200 OK Content-Type: text/html; charset=utf-8 <head> <title>WOWzh啊士大夫</title> </head> <html> <p>Wow, Pythond大跌 Server</p> </html> ''' # Read picture, put into HTTP format f = open('test.jpg', 'rb') pic_content = ''' HTTP/1.x 200 OK Content-Type: image/gif ''' pic_content = pic_content + f.read() f.close() # Configure socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((HOST, PORT)) pic_content2 = read_file("test.jpg") # infinite loop, server forever while True: # 3: maximum number of requests waiting s.listen(3) conn, addr = s.accept() request = conn.recv(1024) method = request.split(' ')[0] src = request.split(' ')[1] print src # deal with GET method if method == 'GET': # ULR if src == '/test.jpg': content = pic_content2 else: content = text_content print 'Connected by', addr print 'Request is:', request #print 'content is:', content if (pic_content == pic_content2): print "==",len(pic_content2),len(pic_content) else: print "!=",len(pic_content2),len(pic_content) with open("a1.txt", 'wb') as f: f.write(pic_content) with open("a2.txt", 'wb') as f: f.write(pic_content2) conn.sendall(content) # close connection conn.close()
t3
最新推荐文章于 2025-05-02 14:29:44 发布