Example: 09-WiFi/http_post.py

# 本作品采用MIT许可证授权。
# 版权所有 (c) 2013-2023 OpenMV LLC。保留所有权利。
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# 使用HTTP/Post请求模块上传文件的示例

import network
import requests

# 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',
    # 如果需要,添加更多头文件
}

# Send some files
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 ""