I have got a SimpleHTTPServer in a raspberry pi working with a python script that is executed in the same directory as the index.html web page. The code is the following:
#!/usr/bin/python
import SimpleHTTPServer
import SocketServer
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import httplib2
PORT = 8080
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
print "serving at port", PORT
httpd.serve_forever()
Once the web page is loaded, it starts to send different get requests with data that I need to read with my python script, but I don know how to do it.
This is an example of the get requests;
10.8.0.6 - - [27/Nov/2016 11:18:07] code 404, message File not found
10.8.0.6 - - [27/Nov/2016 11:18:07] "GET /ok.png HTTP/1.1" 404 -
10.8.0.6 - - [27/Nov/2016 11:18:07] code 404, message File not found
10.8.0.6 - - [27/Nov/2016 11:18:07] "GET /red.png HTTP/1.1" 404 -
10.8.0.6 - - [27/Nov/2016 11:18:07] code 404, message File not found
10.8.0.6 - - [27/Nov/2016 11:18:07] "GET /arduino/start/0.16388046142178503 HTTP/1.1" 404 -
10.8.0.6 - - [27/Nov/2016 11:18:07] code 404, message File not found
10.8.0.6 - - [27/Nov/2016 11:18:07] "GET /favicon.ico HTTP/1.1" 404 -
10.8.0.6 - - [27/Nov/2016 11:18:17] code 404, message File not found
10.8.0.6 - - [27/Nov/2016 11:18:17] "GET /arduino/update/0.6913944096802204 HTTP/1.1" 404 -
Don't worry about the 404 error, it appears because the web has different icons that I did not put in the directory yet because I am doing tests.
The reason because I want to do that it's because I am "translating" a web sever to control a solenoid valve and different sensors that I had working on Arduino Yun. In arduino, the code reads the get requests, and depending on what is the get request asking for it responses with an XML package with the sensor data or it acts on the valve.
On arduino, the code for "catching" the get request as a String is the following;
if (client) { // got client?
String request = client.readString();
request.trim();
And then the program looks for a word in the string and depending on what word is found it responses with different information. An example;
if (request=="inicial") {
// send rest of HTTP header
client.println("Content-Type: text/xml");
client.println("Connection: keep-alive");
client.println();
// send XML file containing input states
inicial(client);
}
Summaraizing, I want to read the get requests as a String in my python script and extract data from them, and then response with an XML, but I think this last step will be better explained on the internet so I don't worry about it.
I hope my explanation was enough clear.
Thanks!!!