python实现静态服务器
本文实例为大家分享了python静态服务器的具体代码,供大家参考,具体内容如下
#coding:utf-8 import socket import multiprocessing import re HTML_ROOT_DIR = "./html" class HTTPServer(object): #初始化 def __init__(self): self.serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) #开启服务器启动进程 def start(self): self.serversocket.listen(128) while True: clintsocket, addree = self.serversocket.accept() hanlderprogess = multiprocessing.Process(target=self.xiexi, args=(clintsocket,)) hanlderprogess.start() clintsocket.close() #绑定端口 def bind(self,port): self.serversocket.bind(("", port)) #获取请求头,进行解析,同时发送相应头 def xiexi(clserver): date = clserver.recv(1024) print date list = str(date).split("\r\n") # 解析 requesthead = list[0] print requesthead #使用正则表达式解析出请求头的文件名称 file = re.match(r"\w+ +(/[^ ]*) ",requesthead).group(1) #对获取的问题名判断,如果是/的就进行显示index.html if "/"==file: file = "/index.html" try: f = open(HTML_ROOT_DIR+file,"rb") except: response_start_line = "HTTP/1.1 200 OK \r\n" response_headers = "Server:My server\r\n" response_body = "the file not file" else: file_date = f.readline() f.close() response_start_line = "HTTP/1.1 200 OK \r\n" response_headers = "Server:My server\r\n" response_body = file_date.decode("utf-8") response = response_start_line+response_headers+"\r\n"+response_body print response clserver.send(bytes(response)) clserver.close() def main(): server = HTTPServer() server.bind(1001) server.start() if __name__=="__main__": main()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。