主要使用python实现了一个http服务器。http服务器实现了用户的注册和登录的简单功能,当然还可以继续扩展。
数据的存储使用的是文件,有兴趣的话可以使用数据库进行存储。当然根据个人兴趣而定。
本记录主要是为了自己后面进行查阅,并给有需要的人提供方便,抛砖引玉。如果浪费了大家的时间也请多多关照。
- 进程启动代码
1 from ChttpServer import CHttpServer 2 def main(): 3 http_server = CHttpServer.CHttpServer() 4 http_server.bind(8000) 5 http_server.start() 6 7 if __name__ == "__main__": 8 main()
2.http服务器实现类
1 import socket 2 import sys 3 import re 4 from multiprocessing import Process 5 class CHttpServer(object): 6 """Httpserver服务端类""" 7 def __init__(self): 8 self.m_serverSocket = socket.socket(socket.AF_INET,socket.SOCK_STREAM) 9 self.m_serverSocket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) 10 def start(self): 11 self.m_serverSocket.listen(128) 12 while(True): 13 m_clientSocket,m_clientAddr = self.m_serverSocket.accept() 14 print('[%s,%s]用户连接上了服务器' % m_clientAddr) 15 handle_client_process = Process(target = self.handle_client,args=(m_clientSocket,)) 16 handle_client_process.start() 17 m_clientSocket.close() 18 #数据处理进程 19 def handle_client(self,clientSocket): 20 ''' 21 处理客户请求操作 22 ''' 23 #获取客户请求数据 24 request_data = clientSocket.recv(1024) 25 #print('接收到的数据:%s' % request_data) 26 request_lines=request_data.splitlines() 27 #print(len(request_lines)) 28 strlist=[] 29 for line in request_lines: 30 strlist=str(line).split('&') 31 if len(strlist) > 1: 32 break 33 #print(strlist[1:5]) 34 userinfolist = strlist[1:3] 35 user_info='&'.join(userinfolist) 36 #print(userinfolist) 37 #用户名&密码&操作类型&来源 38 #解析请求报文 39 ''' 40 报文协议格式:clientparam=&username&pwd&method&type&. 41 type:0 注册 1登录 42 ''' 43 file_data='' 44 response_body='' 45 response='' 46 response_start_line = "HTTP/1.1 200 OK\r\n" 47 response_headers = "Server: My server\r\n" 48 if strlist[3]=='1':#注册 49 """写注册文件""" 50 fp = open('userinfo.txt','a+') 51 52 fp.write(user_info) 53 fp.write('\n') 54 fp.close() 55 file_data=r'<b>恭喜您,注册成功:'+ (strlist[1]+'</b>') 56 response_body = file_data 57 response = response_start_line + response_headers + "\r\n" + response_body 58 elif strlist[3]=='2': #登录 59 """登录系统""" 60 fp = open('userinfo.txt','r') 61 for line in fp.readlines(): 62 if line.replace('\n','') == user_info.replace('\n',''): 63 #print(user_info) 64 file_data=r'<b>恭喜您,登录成功:'+ (strlist[1]+'</b>') 65 response_body = file_data 66 response = response_start_line + response_headers + "\r\n" + response_body 67 break 68 # 向客户端返回 响应数据 69 fp.close() 70 elif strlist[3]=='3': #重置密码 71 pass 72 else: 73 file_data=r'非法操作' 74 response_body = file_data 75 response = response_start_line + response_headers + "\r\n" + response_body 76 print('发送的数据[%s]' % response) 77 clientSocket.send(bytes(response, "gbk")) 78 #组装返回包 79 def start_response(self,status,headers): 80 response_headers = "HTTP/1.1 " + status + "\r\n" 81 for header in headers: 82 response_headers += "%s: %s\r\n" % header 83 self.response_headers = response_headers 84 #绑定端口 85 def bind(self,port): 86 self.m_serverSocket.bind(('',port)) 87 def parseline(self,strcon): 88 """解析字符串""" 89 pass
3、运行情况
测试命令:http://127.0.0.1:8000/&jifei&123456&2&0&