gpt4 book ai didi

python - 获取原始的、未解析的 HTTP 响应

转载 作者:太空狗 更新时间:2023-10-29 20:48:14 27 4
gpt4 key购买 nike

是否有任何直接的方法来发出 HTTP 请求并获得原始的、未解析的响应(特别是 header )?

最佳答案

使用 socket直接模块:

import socket

CRLF = "\r\n"

request = [
"GET / HTTP/1.1",
"Host: www.example.com",
"Connection: Close",
"",
"",
]

# Connect to the server
s = socket.socket()
s.connect(('www.example.com', 80))

# Send an HTTP request
s.send(CRLF.join(request))

# Get the response (in several parts, if necessary)
response = ''
buffer = s.recv(4096)
while buffer:
response += buffer
buffer = s.recv(4096)

# HTTP headers will be separated from the body by an empty line
header_data, _, body = response.partition(CRLF + CRLF)

print header_data
HTTP/1.0 302 Found
Location: http://www.iana.org/domains/example/
Server: BigIP
Connection: Keep-Alive
Content-Length: 0

关于python - 获取原始的、未解析的 HTTP 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8918350/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com