This article mainly records the implementation code that uses Python's requests module to send a GET request.
To send a GET request to the server:
No parameters: R = Requests.get (URL)
With params: R = Requests.get (url,params=params)
With params and headers: R = Requests.get (url,params=params,headers=headers)
The code is as follows:
#Coding=utf-8ImportUnitTestImportRequestsclassgettest (unittest. TestCase):defsetUp (self): host='https://httpbin.org/'Endpoint='Get'Self.url="'. Join ([Host, endpoint])deftest1 (self): U" "get no parameter test" "R1= Requests.get (Self.url)#send a request to the serverCode = R1.status_code#Status CodeSelf.assertequal (200, code)Print(R1.text)#Unicode Type text deftest2 (self): U" "get band parameter test" "params= {'show_env':'1'} R2= Requests.get (self.url,params=params) self.assertequal (200, R2.status_code)deftest3 (self): U" "get with parameters, with headers test" "params= {'show_env':'8'} headers= {'Connection':'keep-alive','accept-encoding':'gzip, deflate', 'Accept':'*/*','user-agent':'python-requests/2.18.3'} R= Requests.get (Self.url, params=params,headers=headers) R3=R.json ()Print(R3) Connect= R3.get ('Headers'). Get ('Connection') self.assertequal ('Close', connect)#断言Check the connection value in the header defTearDown (self):Passif __name__=="__main__": Unittest.main ()
Python interface test-send a GET request using the Requests module