在nginx和uwsgi网站上获取查询字符串

时间:2017-02-06 04:05:15

标签: python nginx server uwsgi

我用python,uwsgi和nginx构建了一个非常小的站点。当我在网络浏览器地址栏中输入localhost:8080之类的“hello world'显示。

接下来我将添加查询字符串处理但不知道如何处理。我想在命令提示符下打印查询字符串,例如。因此,收件人就像那个localhost:8080?abc&id=21&price=778和查询字符串一样abc&id=21&price=778

我不能成功。这里是输出和代码:

(myappenv) gameboy@localhost:~/Dropbox/PycharmProjects/digitalOApp$ uwsgi --ini digitalOApp.ini
[uWSGI] getting INI configuration from digitalOApp.ini
*** Starting uWSGI 2.0.14 (64bit) on [Mon Feb  6 04:54:51 2017] ***
compiled with version: 4.9.2 on 03 February 2017 02:52:59
os: Linux-3.16.0-4-amd64 #1 SMP Debian 3.16.39-1 (2016-12-30)
nodename: localhost
machine: x86_64
clock source: unix
detected number of CPU cores: 2
current working directory: /home/gameboy/Dropbox/PycharmProjects/digitalOApp
detected binary path: /home/gameboy/Dropbox/PycharmProjects/digitalOApp/myappenv/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
your processes number limit is 15353
your memory page size is 4096 bytes
detected max file descriptor number: 65536
lock engine: pthread robust mutexes
thunder lock: enabled
uwsgi socket 0 bound to UNIX address /home/gameboy/Dropbox/PycharmProjects/digitalOApp/digitalOApp.sock fd 3
uwsgi socket 1 bound to TCP address :8080 fd 4
Python version: 3.4.2 (default, Oct  8 2014, 10:47:48)  [GCC 4.9.1]
Python main interpreter initialized at 0x17461e0
python threads support enabled
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 436608 bytes (426 KB) for 5 cores
*** Operational MODE: preforking ***
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x17461e0 pid: 14147 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 14147)
spawned uWSGI worker 1 (pid: 14148, cores: 1)
spawned uWSGI worker 2 (pid: 14149, cores: 1)
spawned uWSGI worker 3 (pid: 14150, cores: 1)
spawned uWSGI worker 4 (pid: 14151, cores: 1)
spawned uWSGI worker 5 (pid: 14152, cores: 1)
None
[pid: 14148|app: 0|req: 1/1] 127.0.0.1 () {36 vars in 646 bytes} [Mon Feb  6 04:55:14 2017] GET /?a=hej => generated 41 bytes in 0 msecs (HTTP/1.1 200) 1 headers in 44 bytes (0 switches on core 0)
None
[pid: 14151|app: 0|req: 1/2] 127.0.0.1 () {36 vars in 620 bytes} [Mon Feb  6 04:55:16 2017] GET /favicon.ico => generated 41 bytes in 0 msecs (HTTP/1.1 200) 1 headers in 44 bytes (0 switches on core 0)

wsgi.py:

import os, sys

def application(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/html')])
    print(os.getenv("QUERY_STRING"))
    h1 = "<h1 style='color:blue'>Hello There!</h1>"
    return(bytes(h1, 'utf-8'))

digitalOApp.ini:

[uwsgi]

module = wsgi:application

master = true
processes = 5

socket = /home/gameboy/Dropbox/PycharmProjects/digitalOApp/digitalOApp.sock

chmod-socket = 664

vacuum = true

die-on-term = true

enable-threads = true
thunder-lock = true

socket = :8080
protocol = http

nginx的:

(myappenv) gameboy@localhost:~/Dropbox/PycharmProjects/digitalOApp$ sudo cat /etc/nginx/sites-enabled/digitalOApp
[sudo] password for gameboy: 
server {
    listen 80;
    server_name localhost;

    location / {
        include         uwsgi_params;
        uwsgi_pass      unix:/home/gameboy/Dropbox/PycharmProjects/digitalOApp/digitalOApp.sock;
    }
}


(myappenv) gameboy@localhost:~/Dropbox/PycharmProjects/digitalOApp$ 

1 个答案:

答案 0 :(得分:3)

替换...

print(os.getenv("QUERY_STRING"))

...与:

print(environ['QUERY_STRING'])