$_SERVER['QUERY_STRING'] 相当于 python

$_SERVER['QUERY_STRING'] equivalent in python

我是一名 PHP 开发人员,我曾经使用 PHP 中的 $_SERVER['QUERY_STRING'] 获取查询字符串。

Python 2.7 语法是什么?

import web
import speech_recognition as sr
from os import path

urls = (
    '/voice', 'Voice'
)
app = web.application(urls, globals())


class Voice:        
   def GET(self):
    WAV_FILE = path.join(path.dirname(path.realpath("C:\Python27")),'wavfile.wav')

    r = sr.Recognizer()
    with sr.WavFile("C:\Python27\wavfile.wav") as source:
     audio = r.record(source) # read the entire WAV file
     output = r.recognize_google(audio)
     return output



if __name__ == "__main__":
    app.run()
import urlparse
url = 'http://example.com/?q=abc&p=123'
par = urlparse.parse_qs(urlparse.urlparse(url).query)

假设您使用的是 web.py(您的代码建议),您可以使用 web.ctx.query(包括 ?)或 web.ctx.env['QUERY_STRING'],后者不:

import web

urls = (
    '/', 'index',
)

class index:
    def GET(self):
        return "web.ctx.env['QUERY_STRING']: {}".format(
            web.ctx.env['QUERY_STRING'])

if __name__ == '__main__':
    app = web.application(urls, globals())
    app.run()

有关更多信息,请参阅 cookbook entry on ctx

http://webpy.org/cookbook/input

user_data = web.input()

或者使用 urlparse 库:

https://docs.python.org/2/library/urlparse.html

from urlparse import urlparse

o = urlparse('http://www.cwi.nl:80/%7Eguido?x=y')