Rakesh K
Rakesh K

Reputation: 712

$_SERVER['QUERY_STRING'] equivalent in python

I am a PHP developer and I used to get query string using $_SERVER['QUERY_STRING'] in PHP.

What is the Python 2.7 syntax for this?

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

Upvotes: 0

Views: 375

Answers (3)

zsolt.k
zsolt.k

Reputation: 703

http://webpy.org/cookbook/input

user_data = web.input()

Or use the urlparse library:

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

from urlparse import urlparse

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

Upvotes: 1

Chris
Chris

Reputation: 136995

Assuming you are using web.py (which your code suggests) you can use web.ctx.query (which includes the ?) or web.ctx.env['QUERY_STRING'], which doesn't:

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

See the cookbook entry on ctx for more.

Upvotes: 0

Ant Avison
Ant Avison

Reputation: 178

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

Upvotes: 0

Related Questions