Processing HTTP GET input parameter on server side in python

前端 未结 3 1067
暖寄归人
暖寄归人 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:16

    BaseHTTPServer is a pretty low-level server. Generally you want to use a real web framework that does this kind of grunt work for you, but since you asked...

    First import a url parsing library. In Python 2,x it's urlparse. (In Python3, you'd use urllib.parse)

    import urlparse
    

    Then, in your do_get method, parse the query string.

    imsi = urlparse.parse_qs(urlparse.urlparse(self.path).query).get('imsi', None)
    print imsi  # Prints None or the string value of imsi
    

    Also, you could be using urllib in your client code and it would probably be a lot easier.

提交回复
热议问题