有些开发环境需要发送html请求以测试代码是否正常运行,但是代码如果被部署在linux服务器上,导致不方便或者无法使用postman时候,可以使用一下代码代替postman。
例如以下的操作,可以使用下列代码替代
import requests # 地址 url = 'http://192.168.x.xx:5010/' # Flask 服务器的设置的实际地址 # 要发送的图像文件路径 image_path1 = r"C:\Users\Admin\Desktop\date_workstation\\U3.jpg" # 构建请求数据 label_name= {'label_name': "2"} files = {'image1': open(image_path1, 'rb')} # 按需求设置 # 发送 POST 请求 response = requests.post(url, data=label_name, files=files) if response.status_code == 200: print('POST request successful') # # 获取响应数据 result = response.json() print('检测结果:', result) else: print('POST request failed with status code:', response.status_code)