0

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()
5
  • 1
    Using any particular framework? Commented Mar 14, 2016 at 19:32
  • No, updated the question. Commented Mar 14, 2016 at 19:42
  • I think the answer is yes to a particular framework. It looks like you're using web.py? Commented Mar 14, 2016 at 20:49
  • I am not aware of web.py as weill. Commented Mar 15, 2016 at 4:41
  • Your code includes import web (which isn't in the Python 2 standard library) and the same urls / app = web.application(urls, globals()) structure from the web.py tutorial. If your code really looks like this I'm fairly confident that it uses web.py. Commented Mar 15, 2016 at 7:18

3 Answers 3

1

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')
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

While this code may answer the question, it would be better to include some context, explaining how it works and when to use it. Code-only answers are not useful in the long run.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.