概述
请求方式:
【数据来自127.0.0.1:38470】
GET / HTTP/1.1
Host: 127.0.0.1:8089
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:58.0) Gecko/20100101 Firefox/58.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Upgrade-Insecure-Requests: 1
应答方式:
Request URL: https://www.baidu.com/
Request Method: GET
Status Code: 200 OK
Remote Address: 182.61.200.7:443
Referrer Policy: unsafe-url
Bdpagetype: 2
Bdqid: 0xb90bd61c00028789
Cache-Control: private
Connection: Keep-Alive
Content-Encoding: gzip
Content-Type: text/html;charset=utf-8
Date: Fri, 02 Aug 2019 02:29:55 GMT
Expires: Fri, 02 Aug 2019 02:29:54 GMT
Server: BWS/1.1
Set-Cookie: BDSVRTM=113; path=/
Set-Cookie: BD_HOME=1; path=/
Set-Cookie: H_PS_PSSID=26523_1455_21085_29522_29519_29099_29567_28837_29221_29072; path=/; domain=.baidu.com
Strict-Transport-Security: max-age=172800
Transfer-Encoding: chunked
X-Ua-Compatible: IE=Edge,chrome=1
使用空格区分header和body
1、简单的http实现应答
import socket
def service(new_socket):
"""为客户端返回数据"""
# 1接受浏览器发送过来的请求,即http请求
# GET /HTTP/1.1
request = new_socket.recv(1024)
# 2返回http格式的数据,给浏览器
# 2.1 准备发送给浏览器的数据--header
response = "HTTP/1.1 200 OKrn"
response += "rn"
# 2.2 准备发送给浏览器的数据--body
response += "该网页被举报,无法打开!"
new_socket.send(response.encode(("utf-8")))# send发送的需要是字节类型的数据
# 关闭套接字
new_socket.close()
print("OK")
def main():
"""用来完成整体控制"""
# 1创建套接字
tcp_server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
# 2帮顶
tcp_server.bind(("",7890))
# 3监听套接字
tcp_server.listen(128)
while True:
# 4等待新客户的链接
new_socket,client_addr = tcp_server.accept()
# 5为这个客户端服务
service(new_socket)
# 关闭监听套接字
tcp_server.close()
if __name__=="__main__":
main()
2、应答html固定页面
import socket
def service(new_socket):
"""为客户端返回数据"""
# 1接受浏览器发送过来的请求,即http请求
# GET /HTTP/1.1
request = new_socket.recv(1024)
print(request)
# 2返回http格式的数据,给浏览器
# 2.1 准备发送给浏览器的数据--header
response = "HTTP/1.1 200 OKrn"
response += "rn"
# 2.2 准备发送给浏览器的数据--body
# response += "aaaaaa"
f = open("./html/index.html","rb")
html_content = f.read()
new_socket.send(response.encode(("utf-8"))) # header
new_socket.send(html_content) # body
# 关闭套接字
new_socket.close()
print("OK")
def main():
"""用来完成整体控制"""
# 1创建套接字
tcp_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp_server.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
# 2帮顶
tcp_server.bind(("", 7099))
# 3监听套接字
tcp_server.listen(128)
while True:
# 4等待新客户的链接
new_socket, client_addr = tcp_server.accept()
# 5为这个客户端服务
service(new_socket)
# 关闭监听套接字
tcp_server.close()
if __name__ == "__main__":
main()
3、返回需求页面,默认主页面index
import socket
import re
def service(new_socket):
"""为客户端返回数据"""
# 1接受浏览器发送过来的请求,即http请求
# GET /HTTP/1.1
request = new_socket.recv(1024).decode("utf-8")
request_lines = request.splitlines()
print("")
print(">"*30)
print(request_lines)
# GET /006.html HTTP/1.1
ret = re.match(r"[^/]+(/[^ ]*)",request_lines[0]) # /只要不是“/”都要,然后/后面遇到空格停止
file_name = ""
if ret:
file_name = ret.group(1)
# 默认访问主页
if file_name == '/':
file_name = "/index.html"
try:
f = open("./html"+file_name,"rb")
except:
response = "HTTP/1.1 404 NO FOUNDrn"
response += "rn"
response += "file not found"
new_socket.send(response.encode(("utf-8")))
else:
html_content = f.read()
f.close()
response = "HTTP/1.1 200 OKrn"
response += "rn"
new_socket.send(response.encode(("utf-8"))) # header
new_socket.send(html_content) # body
# 关闭套接字
new_socket.close()
print("OK")
def main():
"""用来完成整体控制"""
# 1创建套接字
tcp_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp_server.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
# 2帮顶
tcp_server.bind(("", 7099))
# 3监听套接字
tcp_server.listen(128)
while True:
# 4等待新客户的链接
new_socket, client_addr = tcp_server.accept()
# 5为这个客户端服务
service(new_socket)
# 关闭监听套接字
tcp_server.close()
if __name__ == "__main__":
main()
最后
以上就是缥缈西装为你收集整理的HTTP 的简单应答方式的全部内容,希望文章能够帮你解决HTTP 的简单应答方式所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复