Processing HTTP GET input parameter on server side in python

前端 未结 3 1069
暖寄归人
暖寄归人 2020-12-13 19:50

I wrote a simple HTTP client and server in Python for experimenting. The first code snippet below shows how I send an HTTP GET request with a parameter named imsi. In the se

3条回答
  •  天命终不由人
    2020-12-13 20:25

    You can parse the query of a GET request using urlparse, then split the query string.

    from urlparse import urlparse
    query = urlparse(self.path).query
    query_components = dict(qc.split("=") for qc in query.split("&"))
    imsi = query_components["imsi"]
    # query_components = { "imsi" : "Hello" }
    
    # Or use the parse_qs method
    from urlparse import urlparse, parse_qs
    query_components = parse_qs(urlparse(self.path).query)
    imsi = query_components["imsi"] 
    # query_components = { "imsi" : ["Hello"] }
    

    You can confirm this by using

     curl http://your.host/?imsi=Hello
    

提交回复
热议问题