python urllib替代请求.put指挥部?

2024-06-02 05:35:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我想通过urllib python3.6库发送图像数据。 目前,我在请求库的帮助下实现了Python2.7。在

在这段代码中,Id有一种方法可以用urllib替换requests lib。在

import argparse
import io
import os
import sys
import base64
import requests


def read_file_bytestream(image_path):
    data = open(image_path, 'rb').read()
    return data


if __name__== "__main__":
    data=read_file_bytestream("testimg.png")
    requests.put("http.//0.0.0.0:8080", files={'image': data})

Tags: 数据path方法代码图像imageimportid
1条回答
网友
1楼 · 发布于 2024-06-02 05:35:58

这里有一种方法,基本上是从文档中提取的:

import urllib.request

def read_file_bytestream(image_path):
    data = open(image_path, 'rb').read()
    return data

DATA = read_file_bytestream("file.jpg")
req = urllib.request.Request(url='http://httpbin.org/put', data=DATA, method='PUT')
with urllib.request.urlopen(req) as f:
    pass
print(f.status)
print(f.reason)

相关问题 更多 >