天天看點

python發送http請求

#coding=utf-8
import urllib2
import json

body = {
    "id":123456,
    "index":"indexName"
}
url = "http://www.baidu.com"
head = {
    "Content-Type":"application/json"
}
# 發送post請求
request = urllib2.Request(url,json.dumps(body),head)
# 發送get請求
# request = urllib2.Request(url)
response = urllib2.urlopen(request,None,3000)
data = response.read()
print(data)