标签:adr 输入 lnl png rhn seo iar zha nqa
1 ---- 请求行 ---- 2 GET / HTTP/1.1 # GET请求方式 请求资源路径 HTTP协议版本 3 ---- 请求头 ----- 4 Host: www.itcast.cn # 服务器的主机地址和端口号,默认是80 5 Connection: keep-alive # 和服务端保持长连接 6 Upgrade-Insecure-Requests: 1 # 让浏览器升级不安全请求,使用https请求 7 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36 # 用户代理,也就是客户端的名称 8 Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8 # 可接受的数据类型 9 Accept-Encoding: gzip, deflate # 可接受的压缩格式 10 Accept-Language: zh-CN,zh;q=0.9 #可接受的语言 11 Cookie: pgv_pvi=1246921728; # 登录用户的身份标识;cookie可以记录浏览器的一些信息、投放广告、历史浏览记录的显示 12 ---- 空行 ----
1 GET / HTTP/1.1\r\n 2 Host: www.itcast.cn\r\n 3 Connection: keep-alive\r\n 4 Upgrade-Insecure-Requests: 1\r\n 5 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36\r\n 6 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8\r\n 7 Accept-Encoding: gzip, deflate\r\n 8 Accept-Language: zh-CN,zh;q=0.9\r\n 9 Cookie: pgv_pvi=1246921728; \r\n 10 \r\n (请求头信息后面还有一个单独的’\r\n’不能省略) 11
1 ---- 请求行 ---- 2 POST /xmweb?host=mail.itcast.cn&_t=1542884567319 HTTP/1.1 # POST请求方式 请求资源路径 HTTP协议版本 3 ---- 请求头 ---- 4 Host: mail.itcast.cn # 服务器的主机地址和端口号,默认是80 5 Connection: keep-alive # 和服务端保持长连接 6 Content-Type: application/x-www-form-urlencoded # 告诉服务端请求的数据类型 7 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36 # 客户端的名称 8 ---- 空行 ---- 9 ---- 请求体 ---- 10 username=hello&pass=hello # 请求参数
1 POST /xmweb?host=mail.itcast.cn&_t=1542884567319 HTTP/1.1\r\n 2 Host: mail.itcast.cn\r\n 3 Connection: keep-alive\r\n 4 Content-Type: application/x-www-form-urlencoded\r\n 5 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36\r\n 6 \r\n(请求头信息后面还有一个单独的’\r\n’不能省略) 7 username=hello&pass=hello
1 --- 响应行/状态行 --- 2 HTTP/1.1 200 OK # HTTP协议版本 状态码 状态描述 3 --- 响应头 --- 4 Server: Tengine # 服务器名称 5 Content-Type: text/html; charset=UTF-8 # 内容类型 6 Transfer-Encoding: chunked # 发送给客户端内容不确定内容长度,发送结束的标记是0\r\n, Content-Length表示服务端确定发送给客户端的内容大小,但是二者只能用其一。 7 Connection: keep-alive # 和客户端保持长连接 8 Date: Fri, 23 Nov 2018 02:01:05 GMT # 服务端的响应时间 9 --- 空行 --- 10 --- 响应体 --- 11 <!DOCTYPE html><html lang=“en”> …</html> # 响应给客户端的数据
1 HTTP/1.1 200 OK\r\n 2 Server: Tengine\r\n 3 Content-Type: text/html; charset=UTF-8\r\n 4 Transfer-Encoding: chunked\r\n 5 Connection: keep-alive\r\n 6 Date: Fri, 23 Nov 2018 02:01:05 GMT\r\n 7 \r\n(响应头信息后面还有一个单独的’\r\n’不能省略) 8 <!DOCTYPE html><html lang=“en”> …</html>
1 import socket 2 3 if __name__ == ‘__main__‘: 4 # 创建tcp服务端套接字 5 tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 6 # 设置端口号复用, 程序退出端口立即释放 7 tcp_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True) 8 # 绑定端口号 9 tcp_server_socket.bind(("", 9000)) 10 # 设置监听 11 tcp_server_socket.listen(128) 12 while True: 13 # 等待接受客户端的连接请求 14 new_socket, ip_port = tcp_server_socket.accept() 15 # 代码执行到此,说明连接建立成功 16 recv_client_data = new_socket.recv(4096) 17 # 对二进制数据进行解码 18 recv_client_content = recv_client_data.decode("utf-8") 19 print(recv_client_content) 20 with open("static/index.html", "rb") as file: 21 # 读取文件数据 22 file_data = file.read() 23 24 # 响应行 25 response_line = "HTTP/1.1 200 OK\r\n" 26 # 响应头 27 response_header = "Server: PWS1.0\r\n" 28 # 响应体 29 response_body = file_data 30 # 拼接响应报文 31 response_data = (response_line + response_header + "\r\n").encode("utf-8") + response_body 32 # 发送数据 33 new_socket.send(response_data) 34 # 关闭服务与客户端的套接字 35 new_socket.close()
1 import socket 2 3 4 def main(): 5 # 创建tcp服务端套接字 6 tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 7 # 设置端口号复用, 程序退出端口立即释放 8 tcp_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True) 9 # 绑定端口号 10 tcp_server_socket.bind(("", 9000)) 11 # 设置监听 12 tcp_server_socket.listen(128) 13 while True: 14 # 等待接受客户端的连接请求 15 new_socket, ip_port = tcp_server_socket.accept() 16 # 代码执行到此,说明连接建立成功 17 recv_client_data = new_socket.recv(4096) 18 if len(recv_client_data) == 0: 19 print("关闭浏览器了") 20 new_socket.close() 21 return 22 # 对二进制数据进行解码 23 recv_client_content = recv_client_data.decode("utf-8") 24 print(recv_client_content) 25 # 根据指定字符串进行分割, 取请求行 26 request_list = recv_client_content.split(‘\n‘)[0].split() 27 # 获取请求资源路径,对请求行进行切割 28 request_path = request_list[1] 29 print(request_path) 30 # 判断请求的是否是根目录(此根目录时相对运行的.py文件),如果条件成立,指定首页数据返回 31 if request_path == "/": 32 request_path = "/index.html" 33 try: 34 # 动态打开指定文件 35 with open("static" + request_path, "rb") as file: 36 # 读取文件数据 37 file_data = file.read() 38 except Exception as e: 39 # 请求资源不存在,返回404数据 40 # 响应行 41 response_line = "HTTP/1.1 404 Not Found\r\n" 42 # 响应头 43 response_header = "Server: PWS1.0\r\n" 44 with open("static/error.html", "rb") as file: 45 file_data = file.read() 46 # 响应体 47 response_body = file_data 48 # 拼接响应报文 49 response_data = (response_line + response_header + "\r\n").encode("utf-8") + response_body 50 # 发送数据 51 new_socket.send(response_data) 52 else: 53 # 响应行 54 response_line = "HTTP/1.1 200 OK\r\n" 55 # 响应头 56 response_header = "Server: PWS1.0\r\n" 57 # 响应体 58 response_body = file_data 59 # 拼接响应报文 60 response_data = (response_line + response_header + "\r\n").encode("utf-8") + response_body 61 # 发送数据 62 new_socket.send(response_data) 63 finally: 64 # 关闭服务与客户端的套接字 65 new_socket.close() 66 67 if __name__ == ‘__main__‘: 68 main() 69
1 import socket 2 import threading 3 4 # 处理客户端的请求 5 def handle_client_request(new_socket): 6 # 代码执行到此,说明连接建立成功 7 recv_client_data = new_socket.recv(4096) 8 if len(recv_client_data) == 0: 9 print("关闭浏览器了") 10 new_socket.close() 11 return 12 13 # 对二进制数据进行解码 14 recv_client_content = recv_client_data.decode("utf-8") 15 print(recv_client_content) 16 # 根据指定字符串进行分割 17 request_list = recv_client_content.split("\n")[0].split() 18 19 # 获取请求资源路径 20 request_path = request_list[1] 21 print(request_path) 22 23 # 判断请求的是否是根目录,如果条件成立,指定首页数据返回 24 if request_path == "/": 25 request_path = "/index.html" 26 27 try: 28 # 动态打开指定文件 29 with open("static" + request_path, "rb") as file: 30 # 读取文件数据 31 file_data = file.read() 32 except Exception as e: 33 # 请求资源不存在,返回404数据 34 # 响应行 35 response_line = "HTTP/1.1 404 Not Found\r\n" 36 # 响应头 37 response_header = "Server: PWS1.0\r\n" 38 with open("static/error.html", "rb") as file: 39 file_data = file.read() 40 # 响应体 41 response_body = file_data 42 43 # 拼接响应报文 44 response_data = (response_line + response_header + "\r\n").encode("utf-8") + response_body 45 # 发送数据 46 new_socket.send(response_data) 47 else: 48 # 响应行 49 response_line = "HTTP/1.1 200 OK\r\n" 50 # 响应头 51 response_header = "Server: PWS1.0\r\n" 52 53 # 响应体 54 response_body = file_data 55 56 # 拼接响应报文 57 response_data = (response_line + response_header + "\r\n").encode("utf-8") + response_body 58 # 发送数据 59 new_socket.send(response_data) 60 finally: 61 # 关闭服务与客户端的套接字 62 new_socket.close() 63 64 65 # 程序入口函数 66 def main(): 67 # 创建tcp服务端套接字 68 tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 69 # 设置端口号复用, 程序退出端口立即释放 70 tcp_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True) 71 # 绑定端口号 72 tcp_server_socket.bind(("", 9000)) 73 # 设置监听 74 tcp_server_socket.listen(128) 75 76 while True: 77 # 等待接受客户端的连接请求 78 new_socket, ip_port = tcp_server_socket.accept() 79 print(ip_port) 80 # 当客户端和服务器建立连接程,创建子线程 81 sub_thread = threading.Thread(target=handle_client_request, args=(new_socket,)) 82 # 设置守护主线程 83 sub_thread.setDaemon(True) 84 # 启动子线程执行对应的任务 85 sub_thread.start() 86 87 88 if __name__ == ‘__main__‘: 89 main()
1 import socket 2 import threading 3 4 # 定义web服务器类 5 class HttpWebServer(object): 6 def __init__(self): 7 # 创建tcp服务端套接字 8 tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 9 # 设置端口号复用, 程序退出端口立即释放 10 tcp_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True) 11 # 绑定端口号 12 tcp_server_socket.bind(("", 9100)) 13 # 设置监听 14 tcp_server_socket.listen(128) 15 # 保存创建成功的服务器套接字 16 self.tcp_server_socket = tcp_server_socket 17 18 19 # 处理客户端的请求 20 @staticmethod 21 def handle_client_request(new_socket): 22 # 代码执行到此,说明连接建立成功 23 recv_client_data = new_socket.recv(4096) 24 if len(recv_client_data) == 0: 25 print("关闭浏览器了") 26 new_socket.close() 27 return 28 29 30 # 对二进制数据进行解码 31 recv_client_content = recv_client_data.decode("utf-8") 32 print(recv_client_content) 33 # 根据指定字符串进行分割 34 request_list = recv_client_content.split("\n")[0].split() 35 36 37 # 获取请求资源路径 38 request_path = request_list[1] 39 print(request_path) 40 41 42 # 判断请求的是否是根目录,如果条件成立,指定首页数据返回 43 if request_path == "/": 44 request_path = "/index.html" 45 46 47 try: 48 # 动态打开指定文件 49 with open("python" + request_path, "rb") as file: 50 # 读取文件数据 51 file_data = file.read() 52 except Exception as e: 53 # 请求资源不存在,返回404数据 54 # 响应行 55 response_line = "HTTP/1.1 404 Not Found\r\n" 56 # 响应头 57 response_header = "Server: PWS1.0\r\n" 58 with open("python/error.html", "rb") as file: 59 file_data = file.read() 60 # 响应体 61 response_body = file_data 62 63 64 # 拼接响应报文 65 response_data = (response_line + response_header + "\r\n").encode("utf-8") + response_body 66 # 发送数据 67 new_socket.send(response_data) 68 else: 69 # 响应行 70 response_line = "HTTP/1.1 200 OK\r\n" 71 # 响应头 72 response_header = "Server: PWS1.0\r\n" 73 74 75 # 响应体 76 response_body = file_data 77 78 79 # 拼接响应报文 80 response_data = (response_line + response_header + "\r\n").encode("utf-8") + response_body 81 # 发送数据 82 new_socket.send(response_data) 83 finally: 84 # 关闭服务与客户端的套接字 85 new_socket.close() 86 87 # 启动web服务器进行工作 88 def start(self): 89 while True: 90 # 等待接受客户端的连接请求 91 new_socket, ip_port = self.tcp_server_socket.accept() 92 # 当客户端和服务器建立连接程,创建子线程 93 sub_thread = threading.Thread(target=self.handle_client_request, args=(new_socket,)) 94 # 设置守护主线程 95 sub_thread.setDaemon(True) 96 # 启动子线程执行对应的任务 97 sub_thread.start() 98 99 # 程序入口函数 100 def main(): 101 # 创建web服务器对象 102 web_server = HttpWebServer() 103 # 启动web服务器进行工作 104 web_server.start() 105 106 if __name__ == ‘__main__‘: 107 main() 108
1 import socket 2 import threading 3 import sys 4 5 6 # 定义web服务器类class HttpWebServer(object): 7 def __init__(self, port): 8 # 创建tcp服务端套接字 9 tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 10 # 设置端口号复用, 程序退出端口立即释放 11 tcp_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True) 12 # 绑定端口号 13 tcp_server_socket.bind(("", port)) 14 # 设置监听 15 tcp_server_socket.listen(128) 16 # 保存创建成功的服务器套接字 17 self.tcp_server_socket = tcp_server_socket 18 19 # 处理客户端的请求 @staticmethod 20 def handle_client_request(new_socket): 21 # 代码执行到此,说明连接建立成功 22 recv_client_data = new_socket.recv(4096) 23 if len(recv_client_data) == 0: 24 print("关闭浏览器了") 25 new_socket.close() 26 return 27 28 # 对二进制数据进行解码 29 recv_client_content = recv_client_data.decode("utf-8") 30 print(recv_client_content) 31 # 根据指定字符串进行分割 32 request_list = recv_client_content.split(" ", maxsplit=2) 33 34 # 获取请求资源路径 35 request_path = request_list[1] 36 print(request_path) 37 38 # 判断请求的是否是根目录,如果条件成立,指定首页数据返回 39 if request_path == "/": 40 request_path = "/index.html" 41 42 try: 43 # 动态打开指定文件 44 with open("static" + request_path, "rb") as file: 45 # 读取文件数据 46 file_data = file.read() 47 except Exception as e: 48 # 请求资源不存在,返回404数据 49 # 响应行 50 response_line = "HTTP/1.1 404 Not Found\r\n" 51 # 响应头 52 response_header = "Server: PWS1.0\r\n" 53 with open("static/error.html", "rb") as file: 54 file_data = file.read() 55 # 响应体 56 response_body = file_data 57 58 # 拼接响应报文 59 response_data = (response_line + response_header + "\r\n").encode("utf-8") + response_body 60 # 发送数据 61 new_socket.send(response_data) 62 else: 63 # 响应行 64 response_line = "HTTP/1.1 200 OK\r\n" 65 # 响应头 66 response_header = "Server: PWS1.0\r\n" 67 68 # 响应体 69 response_body = file_data 70 71 # 拼接响应报文 72 response_data = (response_line + response_header + "\r\n").encode("utf-8") + response_body 73 # 发送数据 74 new_socket.send(response_data) 75 finally: 76 # 关闭服务与客户端的套接字 77 new_socket.close() 78 79 # 启动web服务器进行工作 80 def start(self): 81 while True: 82 # 等待接受客户端的连接请求 83 new_socket, ip_port = self.tcp_server_socket.accept() 84 # 当客户端和服务器建立连接程,创建子线程 85 sub_thread = threading.Thread(target=self.handle_client_request, args=(new_socket,)) 86 # 设置守护主线程 87 sub_thread.setDaemon(True) 88 # 启动子线程执行对应的任务 89 sub_thread.start() 90 91 92 # 程序入口函数 93 def main(): 94 95 96 print(sys.argv) 97 # 判断命令行参数是否等于2, 98 if len(sys.argv) != 2: 99 print("执行命令如下: python3 xxx.py 8000") 100 return 101 102 103 # 判断字符串是否都是数字组成 104 if not sys.argv[1].isdigit(): 105 print("执行命令如下: python3 xxx.py 8000") 106 return 107 108 109 # 获取终端命令行参数 110 port = int(sys.argv[1]) 111 # 创建web服务器对象 112 web_server = HttpWebServer(port) 113 # 启动web服务器进行工作 114 web_server.start() 115 116 117 118 119 if __name__ == ‘__main__‘: 120 main()
标签:adr 输入 lnl png rhn seo iar zha nqa
原文地址:https://www.cnblogs.com/xiaoxuanchuang/p/10752598.html