forked from binux/libMA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HttpUtils.py
80 lines (70 loc) · 2.22 KB
/
HttpUtils.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib2
import urllib
from CryptUtils import crypt
from cStringIO import StringIO
import gzip
from XML2Object import XML2Object
from config import HTTP_PROXY, BASE_URL, USER_AGENT
if HTTP_PROXY:
buildOpener = lambda: urllib2.build_opener(
urllib2.HTTPCookieProcessor(), urllib2.ProxyHandler({'http': HTTP_PROXY}))
else:
buildOpener = lambda: urllib2.build_opener(urllib2.HTTPCookieProcessor())
__dafault_opener__ = buildOpener()
class App(object):
base_url = BASE_URL
def __init__(self):
self.opener = buildOpener()
def __getattr__(self, name):
url = self.base_url + "/connect/app/"
url += name.replace("__", "#").replace("_", "/").replace("#", "_")
def func(params = {}, opener = None):
if opener == None:
opener = self.opener
return XML2Object(connect(url, params = params, opener = opener))
return func
# urllib2.install_opener(opener)
def _cryptParams(params):
rtn = {}
for k, v in params.items():
if isinstance(v,unicode):
v = v.decode("u8")
rtn[k] = crypt.encode64(str(v))
return rtn
def connect(url, params = {}, opener = None):
if opener == None:
opener = __dafault_opener__
url = crypt.getCryptUrl(url)
headers = {
"User-Agent":
USER_AGENT,
"Accept-Encoding":
"gzip, deflate",
}
params = _cryptParams(params)
request = urllib2.Request(url, urllib.urlencode(params), headers)
response = opener.open(request)
responsion = response.read()
try:
f = StringIO(responsion)
gzipper = gzip.GzipFile(fileobj = f)
data = gzipper.read()
except:
data = responsion
try:
rtn = crypt.decode(data)
except:
lines = filter(lambda s: len(s) > 0, data.split("\r\n"))
rtn = None
for line in lines:
if len(d) % 16 == 0:
try:
rtn = crypt.decode(d)
break
except:
pass
return rtn
def connectPOST(url, opeener, params):
pass