Http请求
带端口的Host地址的Get请求:
import httplib
BODY = "***filecontents***"
conn = httplib.HTTPConnection("IP String", Port_num)
conn.request("GET", "/your_uri")
response = conn.getresponse()
print response.status, response.reason
print response.read()
Https请求
Post请求:
import httplib,
import urllib
import json
host = 'www.google.com'
uri = '/docs'
params = {
"user": "test",
"pwd": "123456"
}
headers = {
'User-Agent': 'python',
'Content-Type': 'application/x-www-form-urlencoded',
}
params = urllib.urlencode(params)
conn = httplib.HTTPSConnection(host)
# 发送请求
conn.request("POST", uri, params, headers)
# 获取结果
response = conn.getresponse()
data = response.read()
print 'Response: ', response.status, response.reason
print 'Data: ', data
修改Header的Post请求:
import httplib,
import urllib
import json
host = 'www.google.com'
uri = '/docs'
params = {
"user": "test",
"pwd": "123456"
}
token = 'abc1234'
params = urllib.urlencode(params)
conn = httplib.HTTPSConnection(host)
# 发送请求
conn.putrequest("POST", uri)
# 设置Header,并装填Post的参数
conn.putheader("authorization", token)
conn._set_content_length(params , "POST")
conn.endheaders(params )
# 获取结果
response = conn.getresponse()
data = response.read()
print 'Response: ', response.status, response.reason
print 'Data: ', data
网友评论