2015-06-21 35 views
0

如何使用python將字符串作爲HTTP請求發送?是這樣的: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

https://docs.python.org/2/library/httplib.html#examples –

+0

http://docs.python-requests.org/en/latest/user/quickstart/ –

回答

0

假設蟒蛇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)