Create HTTP Requests from Python

We will learn how to post a simple request from python.

We are going to work in jupyter notebook. If you have python use should have jupyter notebook too. Run a command line in the folder where your .ipynb file is located, and simpply type in 'jupyter notebook'. A webpage automatically should open, with the files listed from the folder you run the command in. Open the .ipynb.

First of all the imports:

  • import requests

  • import json

For a simple request you don't need anything else.

The code you can see above is in a jupyter notebook cell. You can run each individual cell separately.

file_paths2 is a python dictionary, it contains the number of classes: 2, and the file paths in a python list. In this format we can use this data very easily. We create a try, except block because if we succesfully post the message to Apertus, we don't get feedback.

As you can se posting a request is only 1 line: requests.post( url= "where you want to send the data", json="the data"). The data must be in the above form, because it will automatically convert it to json. We add a 3rd parameter timeout, that is the time we wait for the response, if we get a response we print it, and we print the status too.

If you want to send your data to apertus, you must give the IP adress of the pc where it runs, it wait for messages on the 3000 port, so we add it after the IP, and we must define where to send it in Apertus, for that we write ":3000/api/v1/setfastai" after the IP, because we want the setfastai function to get the data.

Last updated