1

I have used this link and successfully run a python script using uWSGI. Although i just followed the doc line by line.

I have a GPS device which is sending data to a remote server. Document of the same device say that it connect to server using TCP which therefore would be http as simple device like a GPS device would not be able to do https (i hope i am right here.) Now as i have configure my Nginx server to forward all incoming HTTP request to python script for processing via uWSGI.

What i want to do is to simply print the url or query string on the HTML page. As i don't have control on the device side (i can only configure device to send data on a IP + Port), i have no clue how data is coming. Below is my access log

[23/Jan/2016:01:50:32 +0530] "(009591810720BP05000009591810720160122A1254.6449N07738.5244E000.0202007129.7200000000L00000008)" 400 172 "-" "-" "-"

Now i have look at this link on how to get the url parameters values, but i don't have a clue that what is the parameter here.

I tried to modified my wsgi.py file as

import requests
r = requests.get("http://localhost.com/")
# or r = requests.get("http://localhost.com/?") as i am directly routing incoming http request to python script and incoming HTTP request might not have #any parameter, just data #
text1 = r.status_code

def application(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/html')])
    return ["<h1 style='color:blue'>Hello There shailendra! %s </h1>" %(text1)]

but when i restarted nginx, i get internal server error. Can some one help me to understand wrong i am doing here (literally i have no clue about the parameters of the application function. Tried to read this link, but what i get from here is that environ argument take care of many CGI environment variables.)

Can some one please help me to figure out what wrong am i doing and guide me to even a doc or resource.

Thanks.

Community
  • 1
  • 1
srj0408
  • 63
  • 1
  • 10

2 Answers2

1

why are you using localhost ".com" ? Since you are running the webserver on the same machine, you should change the line to

 r = requests.get("http://localhost/")

Also move below lines from wsgi.py and put them in testServerConnection.py

 import requests
 r = requests.get("http://localhost/")
 # or r = requests.get("http://localhost.com/?") as i am directly routing           incoming http request to python script and incoming HTTP request might not have      #any parameter, just data #
 text1 = r.status_code

Start NGINX and you also might have to run (i am not sure uwsgi set up on nginx)

    uwsgi --socket 0.0.0.0:8080 --protocol=http -w wsgi

run testConnection.py to send a test request to localhost webserver and print the response

Manas Marthi
  • 121
  • 6
  • tried in virtual environment with `uwsgi --socket 0.0.0.0:8080 --protocol=http -w wsgi` and then started nginx with localhost:8080, worked fine but when tried to automated the process, didn't worked. Here is the error log `2016/05/23 01:24:39 [error] 5480#0: *32 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:/home/shailendra/myapp/myapp.sock", host: "localhost"` – srj0408 May 22 '16 at 19:56
  • If u are running server on port 8080 then your get command should have url as localhost:8080 – Manas Marthi May 23 '16 at 20:32
1

i got the answer for my question. Basically to process a TCP request, you need to open a socket and accept the TCP request on a specific port (as you specified on the hardware)

import SocketServer

class MyTCPHandler(SocketServer.BaseRequestHandler): 
    def handle(self):
        # self.request is the TCP socket connected to the client
        self.data = self.request.recv(1024).strip()
        print "{} wrote:".format(self.client_address[0])
        #data which is received
        print self.data

if __name__ == "__main__":
    #replace IP by your server IP
    HOST, PORT = <IP of the server>, 8000

    # Create the server, binding to localhost on port 9999
    server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)

    # Activate the server; this will keep running until you
    # interrupt the program with Ctrl-C
    server.serve_forever()

After you get the data, you can do any thing with the data. As my data format was shared in the GPS datasheet, i was able to parse the string and get the Lat and long out of it.

Vélimir
  • 409
  • 3
  • 12
srj0408
  • 63
  • 1
  • 10