【问题标题】:Read and put by chunk with urllib2.urlopen synchronously使用 urllib2.urlopen 同步读取和放置
【发布时间】:2018-03-09 02:16:55
【问题描述】:

我有一个简单的 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(url='http://server/path', data=file, headers=headers) 

如何在块不为空的情况下按块大小(块)同步读取和放置此文件?

【问题讨论】:

    标签: python python-2.7 urllib2 urlopen


    【解决方案1】:

    您要执行的操作称为“流式上传”。请尝试以下操作。

    将文件作为流获取:

    resp = requests.get(url, stream = True)
    

    然后像对象一样发布文件:

    requests.post(url, data= resp.iter_content(chunk_size= 4096))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-24
      • 1970-01-01
      • 2015-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-11
      相关资源
      最近更新 更多