Python 使用urllib2.urlopen按块同步读取和放置
我有一个简单的Python脚本,它应该从HTTP源读取一个文件,并向另一个HTTP源发出PUT请求Python 使用urllib2.urlopen按块同步读取和放置,python,python-2.7,urllib2,urlopen,Python,Python 2.7,Urllib2,Urlopen,我有一个简单的Python脚本,它应该从HTTP源读取一个文件,并向另一个HTTP源发出PUT请求 block_size = 4096 file = urllib2.urlopen('http://path/to/someting.file').read(block_size) headers = {'X-Auth-Token': token_id, 'content-type': 'application/octet-stream'} response = requests.put(
block_size = 4096
file = urllib2.urlopen('http://path/to/someting.file').read(block_size)
headers = {'X-Auth-Token': token_id, 'content-type': 'application/octet-stream'}
response = requests.put(url='http://server/path', data=file, headers=headers)
当块不为空时,如何按块大小(块)同步读取和放置此文件?您要执行的操作称为“流式上载”。尝试以下方法 以流的形式获取文件:
resp = requests.get(url, stream = True)
然后发布类似文件的对象:
requests.post(url, data= resp.iter_content(chunk_size= 4096))
您要做的就是所谓的“流式上载”。尝试以下方法 以流的形式获取文件:
resp = requests.get(url, stream = True)
然后发布类似文件的对象:
requests.post(url, data= resp.iter_content(chunk_size= 4096))