1

如果我使用它,我正在尝试编写简单的上传应用程序

files = {'upfile_0': (open('mplayer.exe', 'rb'))}
fin = r.post(action, files=files)

它可以工作,但整个文件都在 ram 中。

在我阅读有关流式上传的请求文档之后。我想试试...

with open('mplayer.exe') as f:
    requests.post(action, data=f)

但我得到这个错误。谢谢你的帮助。

Traceback (most recent call last):
File "C:\Users\pulpe\Projekty\ulozto.py", line 23, in <module> requests.post(action, data=f)
File "C:\Python33\lib\site-packages\requests\api.py", line 88, in post return request('post', url, data=data, **kwargs)
File "C:\Python33\lib\site-packages\requests\api.py", line 44, in request return session.request(method=method, url=url, **kwargs)
File "C:\Python33\lib\site-packages\requests\sessions.py", line 335, in request resp = self.send(prep, **send_kwargs)
File "C:\Python33\lib\site-packages\requests\sessions.py", line 438, in send r = adapter.send(request, **kwargs)
File "C:\Python33\lib\site-packages\requests\adapters.py", line 292, in send timeout=timeout
File "C:\Python33\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 428, in urlopen body=body, headers=headers)
File "C:\Python33\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 280, in _make_request conn.request(method, url, **httplib_request_kw)
File "C:\Python33\lib\http\client.py", line 1061, in request self._send_request(method, url, body, headers)
File "C:\Python33\lib\http\client.py", line 1099, in _send_request self.endheaders(body)
File "C:\Python33\lib\http\client.py", line 1057, in endheaders self._send_output(message_body)
File "C:\Python33\lib\http\client.py", line 906, in _send_output self.send(message_body)
File "C:\Python33\lib\http\client.py", line 863, in send datablock = data.read(blocksize)
File "C:\Python33\lib\encodings\cp1250.py", line 23, in decode return codecs.charmap_decode(input,self.errors,decoding_table)[0] UnicodeDecodeError: 'charmap' codec can't decode byte 0x90 in position 2: character maps to <undefined>

如果我在打开时使用“rb”。

Traceback (most recent call last):
File "C:\Python33\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 428, in urlopen body=body, headers=headers)
File "C:\Python33\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 280, in _make_request conn.request(method, url, **httplib_request_kw)
File "C:\Python33\lib\http\client.py", line 1061, in request self._send_request(method, url, body, headers)
File "C:\Python33\lib\http\client.py", line 1099, in _send_request self.endheaders(body)
File "C:\Python33\lib\http\client.py", line 1057, in endheaders self._send_output(message_body)
File "C:\Python33\lib\http\client.py", line 906, in _send_output self.send(message_body)
File "C:\Python33\lib\http\client.py", line 868, in send self.sock.sendall(datablock)
ConnectionResetError: [WinError 10054] Stávající připojení bylo vynuceně ukončeno vzdáleným hostitelem

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Python33\lib\site-packages\requests\adapters.py", line 292, in sendtimeout=timeout
File "C:\Python33\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 474, in urlopen raise MaxRetryError(self, url, e)
requests.packages.urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='upload.uloz.to', port=80): Max retries exceeded with url: /upload?user_id=4179756&did=uloz-to&host=www.uloz.to&a=ca92c8242dce5806238751f24114367174cf797d&rk=ocjd5s5ehl&t=1375618794 (Caused by <class 'ConnectionResetError'>: [WinError 10054] Stávající připojení bylo vynuceně ukončeno vzdáleným hostitelem)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Users\pulpe\Projekty\ulozto.py", line 23, in <module> requests.post(action, data=f)
File "C:\Python33\lib\site-packages\requests\api.py", line 88, in post return request('post', url, data=data, **kwargs)
File "C:\Python33\lib\site-packages\requests\api.py", line 44, in request return session.request(method=method, url=url, **kwargs)
File "C:\Python33\lib\site-packages\requests\sessions.py", line 335, in request resp = self.send(prep, **send_kwargs)
File "C:\Python33\lib\site-packages\requests\sessions.py", line 438, in send r = adapter.send(request, **kwargs)
File "C:\Python33\lib\site-packages\requests\adapters.py", line 327, in send raise ConnectionError(e)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='upload.uloz.to', port=80): Max retries exceeded with url: /upload?user_id=4179756&did=uloz-to&host=www.uloz.to&a=ca92c8242dce5806238751f24114367174cf797d&rk=ocjd5s5ehl&t=1375618794 (Caused by <class 'ConnectionResetError'>: [WinError 10054] Stávající připojení bylo vynuceně ukončeno vzdáleným hostitelem)
4

0 回答 0