1.老师给的思路
#tcp socket 服务端 socket = socket.socket() socket.bind() socket.listen() client_socket = socket.accept() while True: p = Process(target=fun, args=()) p.start() client_socket.close() def fun(client_socket): #接受数据 #request_data = recv(1024) #print(request_data) #解析Http报文数据 request_data #提取请求方式 #提取请求路径path HTML_ROOT_DIR = "./html" path = /index.html /login.html try: file = open("index.html") data = file.read() file.close() except IOError: """ HTTP1.1 404 not found not found """ #返回相应数据 """ HTTP1.1 200 OK hello itcast """ #send() #close()
2.自己写程序
2)版本1:主进程程序
from socket import * from multiprocessing import Process def fun(client_socket): pass def main(): #tcp socket 服务端 server_socket = socket(AF_INET,SOCK_STREAM) server_socket.setsockopt(SOL_SOCKET,SO_REUSEADDR,1) #防止端口不能重复访问 server_socket.bind(("",7788)) server_socket.listen(10) client_socket = server_socket.accept() #等待接受电话 while True: p = Process(target=fun, args=(client_socket,)) p.start() client_socket.close() server_socket.close() if __name__ == "__main__": main()
3)版本2:获取http报文头
from socket import * from multiprocessing import Process def fun(client_socket,client_addr): #接受数据 #request_data = recv(1024) #print(request_data) request_data = client_socket.recv(1024) print("[%s] %s"%(client_addr,request_data)) #解析Http报文数据 request_data #提取请求方式 #提取请求路径path #返回相应数据 #send() #close() def main(): #tcp socket 服务端 server_socket = socket(AF_INET,SOCK_STREAM) server_socket.setsockopt(SOL_SOCKET,SO_REUSEADDR,1) #防止端口不能重复访问 server_socket.bind(("",8080)) server_socket.listen(10) while True: client_socket, client_addr = server_socket.accept() # 等待接受电话 p = Process(target=fun, args=(client_socket,client_addr,)) p.start() client_socket.close() server_socket.close() if __name__ == "__main__": main()
4)版本3:获取html的内容
from socket import * from multiprocessing import Process def fun(client_socket,client_addr): #接受数据 #request_data = recv(1024) #print(request_data) request_data = client_socket.recv(1024) print("[%s] %s"%(client_addr,request_data)) request_data = str(request_data) #解析Http报文数据 request_data #提取请求方式 print(request_data[2:5]) request_method = request_data[2:5] #提取请求路径path print(request_data[6:8]) #返回相应数据 error_data = """ HTTP1.1 404 not found not found """ #HTML_ROOT_DIR = "./html" #path = "index.html" if request_method == "GET": try: file = open("./html/index.html") data = file.read() print(data) file.close() except IOError: error_data #send() #close() def main(): #tcp socket 服务端 server_socket = socket(AF_INET,SOCK_STREAM) server_socket.setsockopt(SOL_SOCKET,SO_REUSEADDR,1) #防止端口不能重复访问 server_socket.bind(("",8080)) server_socket.listen(10) while True: client_socket, client_addr = server_socket.accept() # 等待接受电话 p = Process(target=fun, args=(client_socket,client_addr,)) p.start() client_socket.close() server_socket.close() if __name__ == "__main__": main()
5)返回数据失败
from socket import * from multiprocessing import Process def fun(client_socket,client_addr): #接受数据 #request_data = recv(1024) #print(request_data) request_data = client_socket.recv(1024) print("[%s] %s"%(client_addr,request_data)) request_data = str(request_data) #解析Http报文数据 request_data #提取请求方式 print(request_data[2:5]) request_method = request_data[2:5] #提取请求路径path print(request_data[6:8]) #返回相应数据 error_data = """ HTTP1.1 404 not found not found """ #HTML_ROOT_DIR = "./html" #path = "index.html" if request_method == "GET": try: file = open("./html/index.html") data = file.read() print(data) file.close() except IOError: error_data #send() send_data = """ HTTP1.1 200 OK hello itcast """ client_socket.send(send_data) #close() #client_socket.close() def main(): #tcp socket 服务端 server_socket = socket(AF_INET,SOCK_STREAM) server_socket.setsockopt(SOL_SOCKET,SO_REUSEADDR,1) #防止端口不能重复访问 server_socket.bind(("",8080)) server_socket.listen(10) while True: client_socket, client_addr = server_socket.accept() # 等待接受电话 p = Process(target=fun, args=(client_socket,client_addr,)) p.start() client_socket.close() server_socket.close() if __name__ == "__main__": main()
6)返回数据?如何显示
#-*- coding:utf-8 -*- from socket import * from multiprocessing import Process def fun(client_socket,client_addr): #接受数据 #request_data = recv(1024) #print(request_data) request_data = client_socket.recv(1024) print("[%s] %s"%(client_addr,request_data)) request_data = str(request_data) #解析Http报文数据 request_data #提取请求方式 print(request_data[2:5]) request_method = request_data[2:5] #提取请求路径path print(request_data[6:8]) #返回相应数据 error_data = """ HTTP1.1 404 not found not found """ #HTML_ROOT_DIR = "./html" #path = "index.html" if request_method == "GET": try: file = open("./html/index.html") data = file.read() print(data) file.close() except IOError: error_data #send() send_data = """ HTTP1.1 200 OK hello itcast """ client_socket.send(bytes(send_data.encode("utf-8"))) #close() #client_socket.close() def main(): #tcp socket 服务端 server_socket = socket(AF_INET,SOCK_STREAM) server_socket.setsockopt(SOL_SOCKET,SO_REUSEADDR,1) #防止端口不能重复访问 server_socket.bind(("",8080)) server_socket.listen(10) while True: client_socket, client_addr = server_socket.accept() # 等待接受电话 p = Process(target=fun, args=(client_socket,client_addr,)) p.start() client_socket.close() server_socket.close() if __name__ == "__main__": main()
3.老师讲解
1)版本1:大框架
#-*- coding:utf-8 -*- import socket #空行 from multiprocessing import Process #AF_INET 是个常量 server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM) server_socket.bind(("",8080)) server_socket.listen(128) #函数上面有空行2行 def hand_client(client_socket): pass while True: server_socket.accept() client_socket,client_address = server_socket.recv(1024) #赋值的时候有空格 print(client_socket,client_address) hand_client_process = Process(target=hand_client,args=(client_socket,)) #=函数名 不能有空格 hand_client_process.start() client_socket.close() #多进程会复制 client_socket
2)版本2:请求数据
#-*- coding:utf-8 -*- import socket #空行 from multiprocessing import Process #函数上面有空行2行 def hand_client(client_socket): """处理客户端请求""" #推荐"""""" request_data = client_socket.recv(1024) print(request_data) if __name__ == "__main__": #AF_INET 是个常量 server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM) server_socket.bind(("",8080)) server_socket.listen(128) while True: server_socket.accept() client_socket,client_address = server_socket.recv(1024) #赋值的时候有空格 print(client_socket,client_address) hand_client_process = Process(target=hand_client,args=(client_socket,)) #=函数名 不能有空格 hand_client_process.start() client_socket.close() #多进程会复制 client_socket
3)版本3:构造响应数据
#-*- coding:utf-8 -*- import socket #空行 from multiprocessing import Process #函数上面有空行2行 def hand_client(client_socket): """处理客户端请求""" #推荐"""""" request_data = client_socket.recv(1024) print(request_data) response_start_line = "HTTP/1.1 200 OK " response_headers = "Server: My server " response_body = "hello world" response = response_start_line + response_headers + " " +response_body print(response) if __name__ == "__main__": #AF_INET 是个常量 server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM) server_socket.bind(("",8080)) server_socket.listen(128) while True: server_socket.accept() client_socket,client_address = server_socket.recv(1024) #赋值的时候有空格 print(client_socket,client_address) hand_client_process = Process(target=hand_client,args=(client_socket,)) #=函数名 不能有空格 hand_client_process.start() client_socket.close() #多进程会复制 client_socket
4)版本4:返回数据
#-*- coding:utf-8 -*- import socket #空行 from multiprocessing import Process #函数上面有空行2行 def hand_client(client_socket): """处理客户端请求""" #推荐"""""" request_data = client_socket.recv(1024) print(request_data) response_start_line = "HTTP/1.1 200 OK " response_headers = "Server: My server " response_body = "hello world" response = response_start_line + response_headers + " " +response_body print(response) client_socket.send(bytes(response,"utf-8")) client_socket.close() if __name__ == "__main__": #AF_INET 是个常量 server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM) server_socket.bind(("",8080)) server_socket.listen(128) while True: server_socket.accept() client_socket,client_address = server_socket.recv(1024) #赋值的时候有空格 print(client_socket,client_address) hand_client_process = Process(target=hand_client,args=(client_socket,)) #=函数名 不能有空格 hand_client_process.start() client_socket.close() #多进程会复制 client_socket
5)大成版本:优化版
#-*- coding:utf-8 -*- import socket #空行 from multiprocessing import Process #函数上面有空行2行 def hand_client(client_socket): """处理客户端请求""" #推荐"""""" # 获取客户端请求数据 request_data = client_socket.recv(1024) print("request_data:",request_data) # 构造响应数据 # 空一格在加注释 response_start_line = "HTTP/1.1 200 OK " response_headers = "Server: My server " response_body = "hello world" response = response_start_line + response_headers + " " +response_body print("response:",response) # 向客户端返回相应数据 client_socket.send(bytes(response,"utf-8")) # 关闭客户端连接 client_socket.close() if __name__ == "__main__": #AF_INET 是个常量 server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM) server_socket.bind(("",8080)) server_socket.listen(128) while True: client_socket, client_address = server_socket.accept() #赋值的时候有空格 print("[%s,%s]用户连接上了"%client_address) hand_client_process = Process(target=hand_client,args=(client_socket,)) #=函数名 不能有空格 hand_client_process.start() client_socket.close() #多进程会复制 client_socket