标签:服务 var time 交互 doc gecko int gen encode
import socket server = socket.socket() server.bind((‘127.0.0.1‘, 8002)) server.listen(5) while True: conn, client = server.accept() data = conn.recv(1024) # 接收到的消息转化成字符串 data = str(data, encoding=‘utf-8‘) print(data) resquse_list = data.split(‘\r\n‘) # 将请求首行再切分 first_list = resquse_list[0].split(‘ ‘) # 响应头信息,协议类型,200发送成功,\r\n\r\n后可以再加响应体(返回给页面的信息) conn.send(b‘HTTP/. 200 OK\r\nContent-Type:text/html\r\n\r\n‘) if first_list[1] == ‘/qc1‘: with open(‘qc1.html‘, ‘rb‘)as f: data = f.read() conn.send(data) elif first_list[1] == ‘/qc2‘: with open(‘qc2.html‘, ‘rb‘)as f1: data = f1.read() import datetime now = datetime.datetime.now().strftime(‘%Y-%m-%d %X‘) data = data.replace(‘hello‘,now) conn.send(data.encode(‘utf-8‘)) else: conn.send(b‘404‘) conn.close()
------------------------------------------ html文件: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> hello </body> </html>
# 请求首行:请求类型 请求地址 请求协议
GET /index HTTP/1.1\r\n
# 请求头
Host: 127.0.0.1:8001\r\n
Connection: keep-alive\r\n
Cache-Control: max-age=0\r\n
Upgrade-Insecure-Requests: 1\r\n
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36\r\n
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8\r\n
Accept-Encoding: gzip, deflate, br\r\n
Accept-Language: zh-CN,zh;q=0.9\r\n\r\n‘
# 请求体
。。。。
-------------------------------------
GET与POST请求方式
(1)GET提交的数据会放在URL之后,以?分割URL和传输数据,参数之间以&相连,如EditBook?name=test1&id=123456. POST方法是把提交的数据放在HTTP包的请求体中.
(2)GET提交的数据大小有限制(因为浏览器对URL的长度有限制),而POST方法提交的数据没有限制.
(3)GET与POST请求在服务端获取请求数据方式不同
access-control-allow-origin: *
cache-control: public,max-age=25920000
content-encoding: gzip
content-type: application/javascript
date: Sat, 25 Aug 2018 11:31:15 GMT
last-modified: Tue, 09 Feb 2016 15:15:15 GMT
server: Tengine
status: 200
vary: Accept-Encoding
-------------------------------------
响应状态码
协议://IP:端口(80)/路径?name=lqz&age=18
注:?之前的是请求路径,?之后的是请求数据部分,数据以&相连接
标签:服务 var time 交互 doc gecko int gen encode
原文地址:https://www.cnblogs.com/quqinchao/p/9898521.html