0
1

More than 3 years have passed since last update.

requests

Posted at
#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'}

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1