LoginSignup
0
2

More than 5 years have passed since last update.

PythonでのHTTPクライアント

Last updated at Posted at 2017-10-16

PythonでHTTP GET/POSTします

1. PythonでHTTP GET/POSTするクライアント

  • python環境で実行します
  • 必要に応じ、pip3 install urllibします
get.py

import urllib.request
import urllib.parse

#リクエストパラメータをエンコード
params = {"moji_param": "おはよう", # 文字パラメータの例
            "suji_param": 123,     # 数値パラメータの例
            "nested": {
                "nested_param": "ごきげんよう" # ネストしたパラメータの例
            }
        }
encodedParams = urllib.parse.urlencode(params)

#エンコードしたリクエストパラメータを付加してGETリクエストを送付
with urllib.request.urlopen("http://localhost:1880/test12?" + encodedParams) as res:
    html = res.read().decode("utf-8")
    print(html)

post.py

import urllib.request
import urllib.parse

#リクエストパラメータをエンコード
params = {"moji_param": "おはよう", # 文字パラメータの例
            "suji_param": 123,     # 数値パラメータの例
            "nested": {
                "nested_param": "ごきげんよう" # ネストしたパラメータの例
            }
        }
data = urllib.parse.urlencode(params).encode('utf-8')

#リクエスト・レスポンスを用意してpost
request = urllib.request.Request('http://localhost:1880/test10', data)
response = urllib.request.urlopen(request)
print(response.getcode())
html = response.read()
print(html.decode('utf-8'))

2. Node-REDフロー

  • たとえば、ですけど、上記のクライアントからのリクエストを受けるNode-REDのサンプルフローは以下のような感じかと。
[{"id":"d8af2f1d.1179b","type":"http in","z":"b46bcde3.e9962","name":"","url":"/test12","method":"get","upload":false,"swaggerDoc":"","x":130,"y":100,"wires":[["83adb3c3.f5ff58","3e77f42e.8f1a44","58b3b826.c38f9"]]},{"id":"5a3973f4.c79c04","type":"http response","z":"b46bcde3.e9962","name":"","statusCode":"","headers":{},"x":630,"y":80,"wires":[]},{"id":"e3ed6338.fe56b","type":"template","z":"b46bcde3.e9962","name":"htmlを返す","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<html>\n    <body>\n        <h1>\n        祝!開通!!「{{payload}}」が送られました\n        </h1>\n    </body>\n</html>","output":"str","x":490,"y":80,"wires":[["5a3973f4.c79c04"]]},{"id":"83adb3c3.f5ff58","type":"debug","z":"b46bcde3.e9962","name":"","active":true,"console":"false","complete":"payload.moji_param","x":270,"y":240,"wires":[]},{"id":"3e77f42e.8f1a44","type":"function","z":"b46bcde3.e9962","name":"","func":"msg.payload = msg.payload.moji_param;\nreturn msg;","outputs":1,"noerr":0,"x":330,"y":80,"wires":[["e3ed6338.fe56b"]]},{"id":"58b3b826.c38f9","type":"debug","z":"b46bcde3.e9962","name":"","active":true,"console":"false","complete":"false","x":230,"y":180,"wires":[]},{"id":"e593b21f.fc8a1","type":"http in","z":"b46bcde3.e9962","name":"","url":"/test10","method":"post","upload":false,"swaggerDoc":"","x":130,"y":60,"wires":[["3e77f42e.8f1a44","58b3b826.c38f9","83adb3c3.f5ff58"]]}]

スクリーンショット 2017-10-17 18.28.33.png

0
2
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
2