繁体   English   中英

使用Python + Nginx + uWSGI打印HTTP请求的URL参数

[英]printing URL parameters of a HTTP request using Python + Nginx + uWSGI

我使用了此链接,并使用uWSGI成功运行了python脚本。 虽然我只是逐行遵循doc。

我有一个GPS设备正在将数据发送到远程服务器。 同一设备的文档说它使用TCP连接到服务器,因此它将像HTTP设备这样的简单设备作为http连接到GPS设备将无法执行https(我希望我就在这里。)现在,我已经将Nginx服务器配置为将所有传入的HTTP请求转发到python脚本,以通过uWSGI处理。

我想做的就是在HTML页面上简单地打印URL或查询字符串。 由于我无法控制设备端(我只能将设备配置为通过IP +端口发送数据),因此我不知道数据如何发送。 以下是我的访问日志

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

现在我有看这个链接了解如何拿到网址参数的值,但我没有一个线索,什么是参数在这里。

我试图将wsgi.py文件修改为

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)]

但是当我重新启动nginx时,出现internal server error 有人可以帮助我理解我在这里所做的错误吗(从字面上看,我对应用程序函数的参数一无所知。试图阅读此链接 ,但是我从这里得到的是environ参数照顾了许多CGI环境变量。 )

有人可以帮我弄清楚我在做什么错,甚至可以指导我学习文档或资源。

谢谢。

为什么使用localhost “ .com” 由于您是在同一台计算机上运行网络服务器,因此应将行更改为

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

还要从wsgi.py移动到下面的行中,并将它们放在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

启动NGINX,您可能还必须运行(我不确定在nginx上是否设置了uwsgi)

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

运行testConnection.py以将测试请求发送到localhost网络服务器并打印响应

我得到了我问题的答案。 基本上,要处理TCP请求,您需要打开一个套接字并在特定端口上接受TCP请求(如在硬件上指定的那样)

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()

获取数据后,您可以对数据执行任何操作。 由于我的数据格式是在GPS数据表中共享的,因此我能够解析该字符串并获得纬度和长期使用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM