Skip to content

Using Python send http request

use urllib2 lib

import sys, urllib2
# req = urllib2.Request(sys.argv[1])
req = urllib2.Request('http://www.sina.com.cn')
fd = urllib2.urlopen(req)
print "Retrieved", fd.geturl()
info = fd.info()
for key, value in info.items():
    print "%s = %s" % (key, value)

result

Retrieved http://www.sina.com.cn
content-length = 117199
x-cache = HIT from cnc.sy.1cf2.32.spool.sina.com.cn
x-powered-by = shci_v1.03
content-encoding = gzip
age = 3
expires = Mon, 28 Dec 2015 13:59:14 GMT
vary = Accept-Encoding
server = nginx
last-modified = Mon, 28 Dec 2015 13:57:24 GMT
connection = close
cache-control = max-age=60
date = Mon, 28 Dec 2015 13:58:14 GMT
content-type = text/html

use request lib

import requests
r = requests.get('https://www.bing.com/')
r.status_code
r.text