使用 Python + Nginx + uWSGI 打印 HTTP 请求的 URL 参数
printing URL parameters of a HTTP request using Python + Nginx + uWSGI
我已经使用 this link 并成功 运行 一个使用 uWSGI 的 python 脚本。虽然我只是逐行跟着文档。
我有一个 GPS 设备正在向远程服务器发送数据。同一设备的文档说它使用 TCP 连接到服务器,因此它将是 http,因为像 GPS 设备这样的简单设备将无法执行 https(我希望我在这里。)现在我已经配置了我的 Nginx
服务器将所有传入的 HTTP
请求转发到 python 脚本以通过 uWSGI 进行处理。
我想做的是简单地在 HTML 页面上打印 url 或查询字符串。由于我无法控制设备端(我只能将设备配置为在 IP + 端口上发送数据),所以我不知道数据是如何来的。下面是我的访问日志
[23/Jan/2016:01:50:32 +0530] "(009591810720BP05000009591810720160122A1254.6449N07738.5244E000.0202007129.7200000000L00000008)" 400 172 "-" "-" "-"
现在我查看了 this link 如何获取 url 参数值,但我不知道这里的参数是什么。
我试图将我的 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
。有人可以帮助我理解我在这里做的错误吗(从字面上看,我对应用程序函数的参数一无所知。试图阅读 this link,但我从这里得到的是 environ 参数处理许多 CGI环境变量。)
有人能帮我弄清楚我做错了什么,并指导我找到文档或资源吗?
谢谢。
你为什么使用本地主机 ".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
而且你可能还需要 运行(我不确定 uwsgi 在 nginx 上的设置)
uwsgi --socket 0.0.0.0:8080 --protocol=http -w wsgi
运行 testConnection.py 向本地主机网络服务器发送测试请求并打印响应
我得到了问题的答案。基本上要处理 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 数据表中共享,因此我能够解析字符串并从中获取纬度和经度。
我已经使用 this link 并成功 运行 一个使用 uWSGI 的 python 脚本。虽然我只是逐行跟着文档。
我有一个 GPS 设备正在向远程服务器发送数据。同一设备的文档说它使用 TCP 连接到服务器,因此它将是 http,因为像 GPS 设备这样的简单设备将无法执行 https(我希望我在这里。)现在我已经配置了我的 Nginx
服务器将所有传入的 HTTP
请求转发到 python 脚本以通过 uWSGI 进行处理。
我想做的是简单地在 HTML 页面上打印 url 或查询字符串。由于我无法控制设备端(我只能将设备配置为在 IP + 端口上发送数据),所以我不知道数据是如何来的。下面是我的访问日志
[23/Jan/2016:01:50:32 +0530] "(009591810720BP05000009591810720160122A1254.6449N07738.5244E000.0202007129.7200000000L00000008)" 400 172 "-" "-" "-"
现在我查看了 this link 如何获取 url 参数值,但我不知道这里的参数是什么。
我试图将我的 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
。有人可以帮助我理解我在这里做的错误吗(从字面上看,我对应用程序函数的参数一无所知。试图阅读 this link,但我从这里得到的是 environ 参数处理许多 CGI环境变量。)
有人能帮我弄清楚我做错了什么,并指导我找到文档或资源吗?
谢谢。
你为什么使用本地主机 ".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 而且你可能还需要 运行(我不确定 uwsgi 在 nginx 上的设置)
uwsgi --socket 0.0.0.0:8080 --protocol=http -w wsgi
运行 testConnection.py 向本地主机网络服务器发送测试请求并打印响应
我得到了问题的答案。基本上要处理 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 数据表中共享,因此我能够解析字符串并从中获取纬度和经度。