#encoding =utf-8
Import requests
Import JSON
Class HttpClient (object):
def __init__ (self):
Pass
def __post (Self,url,data=none,json=none,**kargs):
Response=requests.post (Url=url,data=data,json=json)
return response
def __get (Self,url,params=none,**kargs):
Response=requests.get (Url=url,params=params)
def request (Self,requestmethod,requesturl,paramstype,requestdata=none,headers=none,cookies=none):
If requestmethod.lower () = = "POST":
if Paramstype = = "Form":
Response=self.__post (Url=requesturl,data=json.dumps (eval (requestData)), headers=headers,cookies=cookies)
return response
elif Paramstype = = ' json ':
Response = Self.__post (Url=requesturl,json=json.dumps (eval (requestData)), headers=headers,cookies=cookies)
return response
elif Requestmethod = = "Get":
if Paramstype = = "url":
Request_url= "%s%s"% (requesturl,requestdata)
Response=self.__get (url=request_url,headers=headers,cookies=cookies)
return response
elif Paramstype = = "params":
Response=self.__get (url=requesturl,params=requestdata,headers=headers,cookies=cookies)
return response
if __name__ = = "__main__":
Hc=httpclient ()
Response=hc.request ("Post", "http://39.106.41.11:8080/register/", "form", ' {"username": "xufengchai6", "Password": " xufengchai121 "," email ":" [email protected] "} ')
Print Response.text
Results:
C:\Python27\python.exe d:/test/interfaceframework_practice1/util/httpclient.py
{"username": "Xufengchai6", "Code": "01"}
Process finished with exit code 0
Python Requests Request encapsulation