Skip to content

Commit 2fe9473

Browse files
committed
更新curl工具类
1 parent a0504fe commit 2fe9473

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

tools/curl.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,27 @@ def post(url, data, with_user_agent=1):
3333
return response
3434

3535

36-
def get_head(url):
36+
def get_headers(url, header_key=None):
3737
"""
3838
仅仅获取头部信息
3939
"""
4040
cmd = 'curl -I %s' % url
41-
response = os.popen(cmd).read()
42-
return response
41+
response = os.popen(cmd).read().strip()
42+
header_list = response.split('\r\n')
43+
print header_list[0]
44+
headers = {}
45+
for item in header_list:
46+
header = item.split(': ')
47+
if len(header) == 2:
48+
if header[0] == 'Set-Cookie' and headers.get('Set-Cookie') is not None:
49+
headers['Set-Cookie'] = ' | '.join([headers.get('Set-Cookie'), header[1].strip()])
50+
else:
51+
headers[header[0]] = header[1].strip()
52+
53+
if header_key is not None:
54+
return headers.get(header_key, None)
55+
else:
56+
return headers
4357

4458

4559
def save_cookie(url):
@@ -61,5 +75,8 @@ def with_cookie(url):
6175

6276

6377
if __name__ == '__main__':
78+
import json
6479
# print get('https://www.baidu.com/s?wd=python')
65-
print post('http://www.wealink.com/passport/login', 'username=123456&password=123456')
80+
# print post('http://www.wealink.com/passport/login', 'username=123456&password=123456')
81+
# print get_headers('http://jump.zhineng.58.com/clk?target=mv7V0A-b5HThmvqfpv--5HTdmYw-w-ICXjF_E1KpuNG7EhnoEy0q5iubpgPGujYQrj01P19knWTQrjbvnHc1PjEYrHT3nWDdPj0h0vQfIjd_pgPYUARhIaubpgPYUHYQPjEvPWcLn19YnHndFMF-uWdCIZwkrBtf0v98PH98mvqVsvF6UhGGmith0vqd0hP-5HDhIh-1Iy-b5HThIh-1Iy-k5HDkni3zn1D8nH9dsWmvFhR8IZwk5HTh0A7zmyYqFh-1ug6YuyOb5yu6UZP-FMK_uWYVniuGUyRG5iudpyEqnWb3nHcQP1TQP1T1P1NhmvQopyEquynQrH--uWcVmHnkmBYYnhmLsyF-mymVuH0QnAcYrjK6nvPBFMKGujYQFhR8IA-b5HEYnWnzPHTYnjm3Pjn1PHDkPau-UMwY0jY1FMKzpyP-5HDYrHmh0hRbpgcqpZwY0jCfsLIfI1NzPi3drjN3shPfUiubpgPkULnqniu1IyFGujYQnH9vn1n3PzuW0hR6IARb5HDYPHNLn1ndnHT1rjTknaukmgF6Uy-b5iubpgPWmHYOPBu1uyQhmvDqrHmhuA-1UAtqnBu1uyQhUAtqnBuQ5HD8njD1nWTLPjbOrjnLn1TvPBukmyI-UMRV5HDhmh-b5HDdnjThIv-_uAP60hEqniu60ZK-Uhwoug-LULFb5HTh0hRWmyQ_uhQfI1YkFh7BIjYznjDdnjnQnRqCIy78uL--g17tnE&adact=3&psid=187378020189612344490821547&entinfo=442325040684335104_3&PGTID=187378020189612344490821547&ClickID=1&iuType=q_2')
82+
print json.dumps(get_headers('http://www.baidu.com'), indent=4)

0 commit comments

Comments
 (0)