How do I use Python's httplib to send a POST to a URL, with a dictionary of parameters?

后端 未结 2 1918
再見小時候
再見小時候 2021-02-18 17:55

I just want a function that can take 2 parameters:

  • the URL to POST to
  • a dictionary of parameters

How can this be done with httplib? thanks.

2条回答
  •  我在风中等你
    2021-02-18 18:39

    A simpler one, using just urllib:

    import urllib
    params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
    f = urllib.urlopen("http://www.example.org/cgi-bin/query", params)
    print f.read()
    

    Found in Python docs for urllib module

提交回复
热议问题