urllib3教程

 

上传文件

#使用multipart/form-data编码方式上传文件,可以使用和传入Form data数据一样的方法进行,并将文件定义为一个元组的形式     (file_name,file_data):
with open('1.txt','r+',encoding='UTF-8') as f:
    file_read = f.read()

r = http.request('POST',
                 'http://httpbin.org/post',
                 fields={'filefield':('1.txt', file_read, 'text/plain')
                         })
print(r.data.decode('unicode_escape'))

#二进制文件
with open('websocket.jpg','rb') as f2:
    binary_read = f2.read()

r = http.request('POST',
                 'http://httpbin.org/post',
                 body=binary_read,
                 headers={'Content-Type': 'image/jpeg'})
#
# print(json.loads(r.data.decode('utf-8'))['data'] )
print(r.data.decode('utf-8'))
原文地址:https://www.cnblogs.com/ccqk/p/15726985.html