-
Notifications
You must be signed in to change notification settings - Fork 18
/
http_request.py
52 lines (45 loc) · 1.62 KB
/
http_request.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
49
50
51
52
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
class http_request:
def __init__(self, cookie=None):
self.set_cookie = None
self.cookies = requests.cookies.RequestsCookieJar()
def post(self, url, body=None):
try:
r = requests.post(url=url, data=body, cookies=self.cookies, timeout=10.0)
resp = {'status': r.status_code}
content = r.text
for c in r.cookies:
self.cookies.set_cookie(c)
except Exception, e:
resp = {'status': 600}
content = 'HttpHelper: %s' % e
return resp, content
def upload(self, url, files):
try:
headers = {'User-Agent': 'Shockwave Flash'}
r = requests.post(url=url, files=files, cookies=self.cookies, headers=headers, timeout=60.0)
resp = {'status': r.status_code}
content = r.text
for c in r.cookies:
self.cookies.set_cookie(c)
except Exception, e:
resp = {'status': 600}
content = 'HttpHelper: %s' % e
return resp, content
def get(self, url, _headers=None):
try:
r = requests.get(url=url, cookies=self.cookies, timeout=10.0)
resp = {'status': r.status_code}
content = r.text
for c in r.cookies:
self.cookies.set_cookie(c)
except Exception, e:
resp = {'status': 600}
content = 'HttpHelper: %s' % e
return resp, content
if __name__ == "__main__":
http = http_request()
resp, ret = http.get("http://www.baidu.com")
print ret