Merge branch 'master' of https://github.com/yumoqing/appPublic
This commit is contained in:
commit
f92fd0b2f4
120
http_client.py
Normal file
120
http_client.py
Normal file
@ -0,0 +1,120 @@
|
||||
import requests
|
||||
|
||||
class NeedLogin(Exception):
|
||||
pass
|
||||
|
||||
class InsufficientPrivilege(Exception):
|
||||
pass
|
||||
|
||||
class HTTPError(Exception):
|
||||
def __init__(self,resp_code):
|
||||
self.resp_code = resp_code
|
||||
Exception.__init__(self)
|
||||
|
||||
def __expr__(self):
|
||||
return f'Exception:return code={self.resp_code}'
|
||||
|
||||
def __str__(self):
|
||||
return f'Exception:return code={self.resp_code}'
|
||||
|
||||
hostsessions = {}
|
||||
|
||||
class HttpClient:
|
||||
def __init__(self):
|
||||
self.s = requests.Session()
|
||||
|
||||
def url2domain(self,url):
|
||||
parts = url.split('/')[:3]
|
||||
pre = '/'.join(parts)
|
||||
return pre
|
||||
|
||||
def _webcall(self,url,method="GET",
|
||||
params={},
|
||||
files={},
|
||||
headers={},
|
||||
stream=False):
|
||||
domain = self.url2domain(url)
|
||||
sessionid = hostsessions.get(domain,None)
|
||||
if sessionid:
|
||||
headers.update({'session':sessionid})
|
||||
|
||||
if method in ['GET']:
|
||||
req = requests.Request(method,url,
|
||||
params=params,headers=headers)
|
||||
else:
|
||||
req = requests.Request(method,url,
|
||||
data=params,files=files,headers=headers)
|
||||
prepped = self.s.prepare_request(req)
|
||||
resp = self.s.send(prepped)
|
||||
if resp.status_code == 200:
|
||||
h = resp.headers.get('Set-Cookie',None)
|
||||
if h:
|
||||
sessionid = h.split(';')[0]
|
||||
hostsessions[domain] = sessionid
|
||||
|
||||
if resp.status_code == 401:
|
||||
print('NeedLogin:',url)
|
||||
raise NeedLogin
|
||||
|
||||
if resp.status_code == 403:
|
||||
raise InsufficientPrivilege
|
||||
|
||||
if resp.status_code != 200:
|
||||
print('Error', url, method,
|
||||
params, resp.status_code,
|
||||
type(resp.status_code))
|
||||
raise HTTPError(resp.status_code)
|
||||
return resp
|
||||
|
||||
def webcall(self,url,method="GET",
|
||||
params={},
|
||||
files={},
|
||||
headers={},
|
||||
stream=False):
|
||||
resp = self._webcall(url,method=method,
|
||||
params=params,
|
||||
files=files,
|
||||
headers=headers,
|
||||
stream=stream)
|
||||
if stream:
|
||||
return resp
|
||||
try:
|
||||
data = resp.json()
|
||||
if type(data) != type({}):
|
||||
return data
|
||||
status = data.get('status',None)
|
||||
if status is None:
|
||||
return data
|
||||
if status == 'OK':
|
||||
return data['data']
|
||||
return data
|
||||
except:
|
||||
return resp.text
|
||||
|
||||
def __call__(self,url,method="GET",
|
||||
params={},
|
||||
headers={},
|
||||
files={},
|
||||
stream=False):
|
||||
return self.webcall(url, method=method,
|
||||
params=params, files=files, headers=headers)
|
||||
|
||||
def get(self, url, params={}, headers={}):
|
||||
return self.__call__(url,method='GET',params=params,
|
||||
headers=headers)
|
||||
def post(self, url, params={}, headers={}, files={}):
|
||||
return self.__call__(url,method='POST',params=params, files=files,
|
||||
headers=headers)
|
||||
|
||||
def put(self, url, params={}, headers={}):
|
||||
return self.__call__(url,method='PUT',params=params,
|
||||
headers=headers)
|
||||
|
||||
def delete(self, url, params={}, headers={}):
|
||||
return self.__call__(url,method='DELETE',params=params,
|
||||
headers=headers)
|
||||
|
||||
def option(self, url, params={}, headers={}):
|
||||
return self.__call__(url,method='OPTION',params=params,
|
||||
headers=headers)
|
||||
|
Loading…
Reference in New Issue
Block a user