Good day! I wrote (more precisely, skopipastil) such a server:

from http.server import HTTPServer, CGIHTTPRequestHandler import time server_address = ('192.168.0.198', 800) httpd = HTTPServer(server_address, CGIHTTPRequestHandler) begin_time = time.asctime() print('Server started on', begin_time) print('Server is running on address', server_address[0], 'on port', server_address[1]) httpd.serve_forever() 

It receives data (GET request) from the ESP8266 card. This data is displayed in the console, but I cannot process it normally (I want to display it on a separate page).

What do you advise?

UPD. From the user's point of view, it should be like this: I went to 192.168.0.198:800 / ... and saw a window with a temperature value (that's what I get with a get request). The server is running at the same address.

  • More specifically, the question is: how to handle a get request that the server receives (or just which module to use for this). Roughly speaking, from the server I need to transfer it to a separate page. I was looking for similar tasks, I found an example of processing forms using cgi, so I used it. If it can be made easier, let us know in which direction to go. Sorry for illiteracy, I am a beginner in this matter :) - soadmized
  • Updated the question. - soadmized
  • No, just a microcontroller with a temperature sensor sends readings using the get method to the address 192.168.0.198:800, on which the server is running. - soadmized

1 answer 1

microcontroller with temperature sensor sends readings by the get method

To create an http server that records the temperature received in the GET request from the microcontroller and shows it later in subsequent requests, you can use the bottle library :

 #!/usr/bin/env python from bottle import route, request, run, template # $ pip install bottle temperature = None @route('/') def index(): global temperature temperature = request.query.temperature or temperature return template('<b>Temperature: {{temperature}}</b>', temperature=temperature) run(host='localhost', port=8000) 

When you run this Python script, an http server is started, which listens on the local machine on port 8000.

When a GET request containing a temperature parameter is received, the global temperature variable is updated and immediately displayed as html. If the temperature parameter is not specified, then the previous value is returned or None , if the temperature parameter has not been specified yet.

That is, it is expected that the part reading the temperature from the sensor performs queries of the type:

 $ http ':8000?temperature=20' 

which sets the value (20).

To read this value, it is enough to omit the parameter:

 $ http :8000 

You can see in the browser (the same request):

 $ python -mwebbrowser http://localhost:8000 

http command can be obtained by installing the httpie library :

 $ pip install httpie 

Usually, instead of using global variables, a database is used (which allows you to save the temperature value between the server process runs or its instances in different processes). In the simplest case, a simple flat text file can be used. Here is an example c sqlite .

If you do not want to put the bottle using the command (preferably inside virtualenv):

 $ pip install bottle 

it is enough to download bottle.py and put it next to the server code.