-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHttpUtil.py
More file actions
64 lines (53 loc) · 1.8 KB
/
HttpUtil.py
File metadata and controls
64 lines (53 loc) · 1.8 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
# -*- coding:utf-8 -*-
import sys
sys.path.append('../')
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
import gzip, json, urllib, urllib2, collections, time, logging
import json
import ssl
context = ssl._create_unverified_context()
def http_get(url, params={}, header={}):
httpUrl = url
if params is not None and len(params) > 0:
httpUrl = url + "?" + _encode_params(**params)
httpUrl = httpUrl.replace(': ', ':')
httpUrl = httpUrl.replace(', ', ',')
httpUrl = httpUrl.replace("'", '"')
# print httpUrl
req = urllib2.Request(httpUrl, None, headers=header)
res = urllib2.urlopen(req)
body = _read_body(res)
logging.info('-----> body: ' + body)
return body
def http_post(url, params={}, header={}):
params = json.dumps(params)
print 'param : ', params
req = urllib2.Request(url, params, headers=header)
res = urllib2.urlopen(req, context=context)
body = _read_body(res)
return body
def _encode_params(**kw):
params = []
for k, v in kw.iteritems():
if isinstance(v, basestring):
qv = v.encode('utf-8') if isinstance(v, unicode) else v
params.append('%s=%s' % (k, urllib.quote(qv)))
elif isinstance(v, collections.Iterable):
for i in v:
qv = i.encode('utf-8') if isinstance(i, unicode) else str(i)
params.append('%s=%s' % (k, urllib.quote(qv)))
else:
qv = str(v)
params.append('%s=%s' % (k, urllib.quote(qv)))
return '&'.join(params)
def _read_body(res):
using_gzip = res.headers.get('Content-Encoding', '') == 'gzip'
body = res.read()
if using_gzip:
gzipper = gzip.GzipFile(fileobj=StringIO(body))
body = gzipper.read()
gzipper.close()
return body