Python | Use of the requests module

Use of the requests module

In addition to using the browser to send requests to the server, we can also use the third-party module requests to send requests to the server with code and get the results.

# The requests module is a third-party module that can be used to send network connections
# pip install requests

import requests

response = requests.get('http://127.0.0.1:8090')
# print(response) The result is a Response object

# content refers to the returned result, which is a binary and can be used to transfer pictures
# print(response.content.decode('utf8')) # decode binary into string

# The obtained result is a text
print(response. text)

print(response. status_code) # 200

# If the returned result is a json string, the json string can be parsed
# print(response. json())

r = requests.get('http://127.0.0.1:8090/test')
t = r.text # Get the json string
print(t, type(t)) # {"name": "zhangsan", "age": 18}

j = r.json() # parse the json string into the corresponding data type in python
print(j, type(j)) # {'name': 'zhangsan', 'age': 18}

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone Contact Us