我是一名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()
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')
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()
有关详细信息,请参阅ctx
上的食谱条目。