備忘録

備忘録

PythonでPOSTする

Ⅰ. はじめに

PythonでPOSTする方法です。

Ⅱ. プログラム

import requests

# InsecureRequestWarning を非表示にする場合
# from requests.packages.urllib3.exceptions import InsecureRequestWarning
# requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

s = requests.session()

proxies = {
  'http':'http://user:pass@127.0.0.1:8008',
  'https':'https://127.0.0.1:8008'
}

headers = {
  'User-Agent' : 'Mozilla/...',
  # 'Content-Type' : 'application/x-www-form-urlencoded'
}

params = {
  'param1' : 'xxx',
  'param2' : 'yyy'
}

s.proxies = proxies
# 証明書チェックを省く
s.verify = False
s.headers = headers

response = s.post('https://example.com/', params)

# 文字化けする場合はcmdで「chcp 65001」を実行すると治る(Windows)
# 65001 はutf-8
print response.content

Ⅲ. その他

開発環境でMITMを利用したproxy(FiddlerやBurpSuiteやCharlesやmitmproxyなど)
SSL Decryptを有効にしていると以下のエラーが出ます。

requests.exceptions.SSLError:
("bad handshake: Error([('SSL routines', 'ssl3_get_server_certificate', 'certificate verify failed')],)",)
s.verify = False

にするとでなくなります。