http_post.py

# この作品はMITライセンスの下で提供されています。
# Copyright (c) 2013-2023 OpenMV LLC. 全著作権所有。
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# HTTP/Postリクエストモジュールでファイルを送信する例

import network
import requests
import time

# APの情報
SSID = ""  # ネットワークSSID
KEY = ""  # ネットワークキー
URL = 'http://192.168.1.102:8080/upload'

# wlanモジュールを初期化してネットワークに接続します
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, KEY)

while not wlan.isconnected():
    print('Trying to connect to "{:s}"...'.format(SSID))
    time.sleep_ms(1000)

# これでDHCP経由の有効なIPが取得できているはずです
print("WiFi Connected ", wlan.ifconfig())

headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:55.0) Gecko/20100101 Firefox/55.0',
    # 必要であればヘッダーを追加します
}

# ファイルを送信します
files = {
    'image1': ('example1.jpg', open('example1.jpg', 'rb')),
    'image2': ('example2.jpg', open('example2.jpg', 'rb')),
}

r = requests.post(URL, files=files, headers=headers, auth=('admin', 'testadmin'))
# またはJSONデータを送信します
# r = requests.post(URL, json={'some': 'data'}, headers=headers, auth=('admin', 'testadmin'))

print(r.status_code, r.reason)
print(r.headers, r.content)

results matching ""

    No results matching ""