HTTP Protocol (9) use of Python requests Module
Through the requests module in Python, you can also send HTTP requests and receive HTTP responses, thus achieving some more flexible operations.
Requests is a third-party library, but it already comes with this module in Kali. The usage of Python3 is slightly different from that of Python2, so let's take Python2 as an example.
Root@kali:~# python
Python 2.7.15 (default, Jul 28 2018, 11:29:29) [GCC 8.1.0] on linux2Type "help", "copyright", "credits" or "license" for more information. > import requests
Let's take the Get and Post methods in Bugku as examples to introduce the use of the requests module.
1.Get request
Using the get method in the requests module, send the Get request to the target url, assign the result to the variable R1, look at the value of R1 directly, and the status code will be displayed. Check the text property to get the HTTP response body. The newline characters can be parsed through the output of the print () function.
> r1=requests.get (url=' http://123.206.87.240:8002/get/')>>> R1 > r1.textu "$what=$_GET ['what']]
\ r\ necho $what
\ r\ nif ($what=='flag')
\ r\ necho 'flag {* *}'
\ r\ n\ r\ n\ r\ n "> > print (r1.text) $what=$_GET ['what']
Echo $what
If ($what=='flag')
Echo 'flag {* *}'
The following is to send a Get request with parameters, which are expressed in a dictionary:
> r1=requests.get (url=' http://123.206.87.240:8002/get/',params={'what':'flag'})>>> print (r1.text) $what=$_GET ['what']
Echo $what
If ($what=='flag')
Echo 'flag {* *}'
Flagflag {bugku_get_su8kej2en}
2.Post request
Still send the Post request to the target url and store the result in the variable R2:
> r2=requests.post (url=' http://123.206.87.240:8002/post/')>>> print (r2.text) $what=$_POST ['what']
Echo $what
If ($what=='flag')
Echo 'flag {* *}'
Send a Post request with parameters:
> r2=requests.post (url=' http://123.206.87.240:8002/post/',data={'what':'flag'})>>> print (r2.text) $what=$_POST ['what']
Echo $what
If ($what=='flag')
Echo 'flag {* *}'
Flagflag {bugku_get_ssseint67se}
3. Check the header
Check the headers property to get the response header, and you can see that the information in the response header is stored in the form of a dictionary:
> > r1.headers {'Content-Encoding':' gzip', 'Transfer-Encoding':' chunked', 'Keep-Alive':' timeout=60', 'Server':' nginx', 'Connection':' keep-alive', 'Date':' Tue, 04 Dec 2018 23:12:33 GMT', 'Content-Type':' text/html'}
Traverse the keys in the dictionary through the for loop:
> Print (key)... ServerDateContent-TypeTransfer-EncodingConnectionKeep-AliveContent-Encoding
Traverse keys and values:
> Print (key, r1.headers [key]). ('Server',' nginx') ('Date',' Tue, 04 Dec 2018 23:12:33 GMT') ('Content-Type',' text/html') ('Transfer-Encoding',' chunked') ('Connection',' keep-alive') ('Keep-Alive',' timeout=60') ('Content-Encoding',' gzip')
View the specified key value:
> > r1.headers ['Server']' nginx'
Check the request.headers property to get the request header:
> r1.request.headers {'Connection':' keep-alive', 'Accept-Encoding':' gzip, deflate', 'Accept':' * / *', 'User-Agent':' python-requests/2.18.4'}