pythonweb服务器编程(三)
Web静态服务器-2-显示需要的页面
#coding=utf-8 import socket from multiprocessing import Process import re def handleClient(clientSocket): '用一个新的进程,为一个客户端进行服务' recvData = clientSocket.recv(2014) requestHeaderLines = recvData.splitlines() for line in requestHeaderLines: print(line) httpRequestMethodLine = requestHeaderLines[0] getFileName = re.match("[^/]+(/[^ ]*)", httpRequestMethodLine).group(1) print("file name is ===>%s"%getFileName) #for test if getFileName == '/': getFileName = documentRoot + "/index.html" else: getFileName = documentRoot + getFileName print("file name is ===2>%s"%getFileName) #for test try: f = open(getFileName) except IOError: responseHeaderLines = "HTTP/1.1 404 not found\r\n" responseHeaderLines += "\r\n" responseBody = "====sorry ,file not found====" else: responseHeaderLines = "HTTP/1.1 200 OK\r\n" responseHeaderLines += "\r\n" responseBody = f.read() f.close() finally: response = responseHeaderLines + responseBody clientSocket.send(response) clientSocket.close() def main(): '作为程序的主控制入口' serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) serverSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) serverSocket.bind(("", 7788)) serverSocket.listen(10) while True: clientSocket,clientAddr = serverSocket.accept() clientP = Process(target = handleClient, args = (clientSocket,)) clientP.start() clientSocket.close() #这里配置服务器 documentRoot = './html' if __name__ == '__main__': main()
Web静态服务器-3-使用类
#coding=utf-8 import socket import sys from multiprocessing import Process import re class WSGIServer(object): addressFamily = socket.AF_INET socketType = socket.SOCK_STREAM requestQueueSize = 5 def __init__(self, server_address): #创建一个tcp套接字 self.listenSocket = socket.socket(self.addressFamily,self.socketType) #允许重复使用上次的套接字绑定的port self.listenSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) #绑定 self.listenSocket.bind(server_address) #变为被动,并制定队列的长度 self.listenSocket.listen(self.requestQueueSize) def serveForever(self): '循环运行web服务器,等待客户端的链接并为客户端服务' while True: #等待新客户端到来 self.clientSocket, client_address = self.listenSocket.accept() #方法2,多进程服务器,并发服务器于多个客户端 newClientProcess = Process(target = self.handleRequest) newClientProcess.start() #因为创建的新进程中,会对这个套接字+1,所以需要在主进程中减去依次,即调用一次close self.clientSocket.close() def handleRequest(self): '用一个新的进程,为一个客户端进行服务' recvData = self.clientSocket.recv(2014) requestHeaderLines = recvData.splitlines() for line in requestHeaderLines: print(line) httpRequestMethodLine = requestHeaderLines[0] getFileName = re.match("[^/]+(/[^ ]*)", httpRequestMethodLine).group(1) print("file name is ===>%s"%getFileName) #for test if getFileName == '/': getFileName = documentRoot + "/index.html" else: getFileName = documentRoot + getFileName print("file name is ===2>%s"%getFileName) #for test try: f = open(getFileName) except IOError: responseHeaderLines = "HTTP/1.1 404 not found\r\n" responseHeaderLines += "\r\n" responseBody = "====sorry ,file not found====" else: responseHeaderLines = "HTTP/1.1 200 OK\r\n" responseHeaderLines += "\r\n" responseBody = f.read() f.close() finally: response = responseHeaderLines + responseBody self.clientSocket.send(response) self.clientSocket.close() #设定服务器的端口 serverAddr = (HOST, PORT) = '', 8888 #设置服务器服务静态资源时的路径 documentRoot = './html' def makeServer(serverAddr): server = WSGIServer(serverAddr) return server def main(): httpd = makeServer(serverAddr) print('web Server: Serving HTTP on port %d ...\n'%PORT) httpd.serveForever() if __name__ == '__main__': main()
相关推荐
SoarFly00 2020-02-11
OwenJi 2019-12-02
leodengzx 2019-12-01
Westdoor 2019-08-25
huangzonggui 2014-05-29
lovelily 2014-12-15
ROES 2016-06-17
编程相关文章收集 2014-05-30
liboxue 2009-09-16
王道立心 2019-06-29
xasdfg 2015-05-06
baihehua00 2011-12-09
ARCHYang 2015-11-29
ARCHYang 2013-05-31
aluminiumfoil 2011-07-07
服务器端攻城师 2018-12-06
missshy 2018-12-05
JESSIEfoo 2018-09-12