Python - 将 http 请求作为字符串发送
Python - sending http requests as strings
如何使用 python 将 HTTP 请求作为字符串发送?像这样:
r = """GET /hello.htm HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.whosebug.com
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive"""
answer = send(r)
print answer # gives me the response as string
假设python3,建议您使用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)
如何使用 python 将 HTTP 请求作为字符串发送?像这样:
r = """GET /hello.htm HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.whosebug.com
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive"""
answer = send(r)
print answer # gives me the response as string
假设python3,建议您使用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)