#pip intall requests
import requests
payload = {'key1': 'value1', 'key2': 'value2'}
#1秒経過してもサーバーから結果が返ってこないときはエラーが出る
r = requests.get('http://httpbin.org/get', params=payload, timeout=1)
#r = requests.post('http://httpbin.org/post', data=payload)
#r = requests.put('http://httpbin.org/put', data=payload)
#r = requests.delete('http://httpbin.org/delete', data=payload)
print(r.status_code)
print(r.text)
print(r.json())
出力:
200
{
"args": {
"key1": "value1",
"key2": "value2"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.22.0",
"X-Amzn-Trace-Id": "Root=1-5e682022-f3f210e8d7f8b036364f33b8"
},
"origin": "118.158.151.210",
"url": "http://httpbin.org/get?key1=value1&key2=value2"
}
{'args': {'key1': 'value1', 'key2': 'value2'}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.22.0', 'X-Amzn-Trace-Id': 'Root=1-5e682022-f3f210e8d7f8b036364f33b8'}, 'origin': '118.158.151.210', 'url': 'http://httpbin.org/get?key1=value1&key2=value2'}