python requests localhost

Python Requests Localhost

If you are working with Python and want to make HTTP requests to a server running on your local machine, you can use the Python Requests library. This library allows you to send HTTP/1.1 requests extremely easily. You can install this library using pip.

Example Code


import requests

url = "http://localhost:8000"
response = requests.get(url)

print(response.status_code)
print(response.json())

In this example, we are making a GET request to a server running on localhost at port 8000. We are then printing out the response status code and the JSON response content.

Alternative Code

If you want to make a POST request to a server running on localhost, you can use the following code:


import requests

url = "http://localhost:8000"
data = {"key": "value"}
response = requests.post(url, data=data)

print(response.status_code)
print(response.json())

In this example, we are making a POST request to a server running on localhost at port 8000. We are passing in a dictionary of data and then printing out the response status code and the JSON response content.