0

I have a simple server that will return JSON to a user. I want the to provide some inputs to the service, so I use query parameters:

/path?paramName=paramValue&paramName2=paramValue2....&paramNamen=paramValuen

In Python, what is the best way of parsing out these parameters?

My server is a threaded server defined as such:

class ThreadingSimpleServer(SocketServer.ThreadingMixIn,
                            BaseHTTPServer.HTTPServer):
    """ simple threaded server """
    pass

In my request handler, I implemented a do_GET().

Should I have this function split based on the ? to separate out the path from the parameters and then split again on the & or is there a better way to do this?

0

1 Answer 1

1

Use the functions from urllib.parse:

import urllib.parse

u = "http://java.dzone.com/articles/python-201-decorators?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+zones%2Fpython+%28Python+Zone%29"
p = urllib.parse.urlparse(u)
q = urllib.parse.parse_qs(p.query)
print(q)

Output:

{'utm_campaign': ['Feed: zones/python (Python Zone)'],
 'utm_medium': ['feed'],
 'utm_source': ['feedburner']}
Sign up to request clarification or add additional context in comments.

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.