forked from qiyeboy/SpiderBook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.2.2.py
48 lines (42 loc) · 1021 Bytes
/
3.2.2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#coding:utf-8
'''
httplib/urllib实现:GET请求
import httplib
conn =None
try:
conn = httplib.HTTPConnection("www.zhihu.com")
conn.request("GET", "/")
response = conn.getresponse()
print response.status, response.reason
print '-' * 40
headers = response.getheaders()
for h in headers:
print h
print '-' * 40
print response.msg
except Exception,e:
print e
finally:
if conn:
conn.close()
'''
'''
httplib/urllib实现:POST请求
import httplib, urllib
conn = None
try:
params = urllib.urlencode({'name': 'qiye', 'age': 22})
headers = {"Content-type": "application/x-www-form-urlencoded"
, "Accept": "text/plain"}
conn = httplib.HTTPConnection("www.zhihu.com", 80, timeout=3)
conn.request("POST", "/login", params, headers)
response = conn.getresponse()
print response.getheaders() #获取头信息
print response.status
print response.read()
except Exception, e:
print e
finally:
if conn:
conn.close()
'''