Python中的简单URL GET / POST功能 [英] Simple URL GET/POST function in Python
本文介绍了Python中的简单URL GET / POST功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我似乎无法谷歌,但我想要一个执行此功能的功能:
I can't seem to Google it, but I want a function that does this:
接受3个参数(或更多,无论如何):
Accept 3 arguments (or more, whatever):
- URL
- 参数字典
- POST或GET
将结果和响应代码返回给我。
Return me the results, and the response code.
是否有片段这样做?
推荐答案
请求
https://github.com/kennethreitz/requests/
以下是一些使用它的常用方法:
Here's a few common ways to use it:
import requests
url = 'https://...'
payload = {'key1': 'value1', 'key2': 'value2'}
# GET
r = requests.get(url)
# GET with params in URL
r = requests.get(url, params=payload)
# POST with form-encoded data
r = requests.post(url, data=payload)
# POST with JSON
import json
r = requests.post(url, data=json.dumps(payload))
# Response, status etc
r.text
r.status_code
httplib2
https://github.com/jcgregorio/httplib2
>>> from httplib2 import Http
>>> from urllib import urlencode
>>> h = Http()
>>> data = dict(name="Joe", comment="A test comment")
>>> resp, content = h.request("http://bitworking.org/news/223/Meet-Ares", "POST", urlencode(data))
>>> resp
{'status': '200', 'transfer-encoding': 'chunked', 'vary': 'Accept-Encoding,User-Agent',
'server': 'Apache', 'connection': 'close', 'date': 'Tue, 31 Jul 2007 15:29:52 GMT',
'content-type': 'text/html'}
这篇关于Python中的简单URL GET / POST功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文