0

I want to send image data via urllib python 3.6 library. i presently have an implementation of a python 2.7 with the help of requests library.

Id there a way to replace the requests lib with the urllib in this code.

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}) 

1 Answer 1

3

Here is one way, pretty much taken from the docs:

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) 
Sign up to request clarification or add additional context in comments.

2 Comments

It should work in theory, but somehow I'm uploading empty files (size 0) under the correct file name. I wonder if anyone faced the same issue.
@hoohoo-b File permissions?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.