$ _SERVER [' QUERY_STRING']等效于python

时间:2016-03-14 19:31:20

标签: php python python-2.7 web.py

我是一名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()

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

import urlparse
url = 'http://example.com/?q=abc&p=123'
par = urlparse.parse_qs(urlparse.urlparse(url).query)

答案 2 :(得分:0)

假设您使用的是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()

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.andrewfinlay.vectorcalculator">

    <application
        android:allowBackup="true"
        android:fullBackupContent="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:launchMode="singleTask"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SettingsActivity"
            android:label="@string/title_activity_settings"
            android:launchMode="singleTask"
            android:theme="@style/AppTheme.NoActionBar"></activity>
        <activity
            android:name=".AboutActivity"
            android:label="@string/title_activity_about"
            android:launchMode="singleTask"
            android:theme="@style/AppTheme.NoActionBar"></activity>
        <activity
            android:name=".ThemeActivity"
            android:label="@string/title_activity_theme"
            android:theme="@style/AppTheme.NoActionBar"></activity>
    </application>

</manifest>

有关详情,请参阅cookbook entry on ctx