如何使用python将HTTP请求作为字符串发送?像这样的东西:
r = """GET /hello.htm HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.stackoverflow.com
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive"""
answer = send(r)
print answer # gives me the response as string
答案 0 :(得分:0)
假设python 3,建议您使用urllib.request。 但由于您特别要求将HTTP消息作为字符串提供, 我假设你想做低级别的东西,所以你也可以使用http.client:
import http.client
connection = http.client.HTTPConnection('www.python.org')
connection.request('GET', '/')
response = connection.getresponse()
print(response.status)
print(response.msg)
answer = response.read()
print(answer)