Simple URL GET/POST function in Python

前端 未结 5 1658
旧时难觅i
旧时难觅i 2020-11-28 18:52

I can\'t seem to Google it, but I want a function that does this:

Accept 3 arguments (or more, whatever):

  • URL
  • a dictionary of params
5条回答
  •  无人及你
    2020-11-28 19:32

    You could use this to wrap urllib2:

    def URLRequest(url, params, method="GET"):
        if method == "POST":
            return urllib2.Request(url, data=urllib.urlencode(params))
        else:
            return urllib2.Request(url + "?" + urllib.urlencode(params))
    

    That will return a Request object that has result data and response codes.

提交回复
热议问题