2016-05-05 2 views
0

env: Mac OS X El Capitan/python 3.5.1fichier volumineux (environ 3 Go) télécharger avec urllib/sock.sendall (données) OSError

Je veux télécharger le fichier qui est sur la taille 3Go.

def read_in_chunks(file_object, chunk_size=4096): 
    while True: 
     data = file_object.read(chunk_size) 
     if not data: 
      break 
     yield data 

with open('3GB.mov', 'br') as f: 
    data = b''.join([chunk for chunk in read_in_chunks(f)]) 

req = urllib.request.Request(url, data, headers) 
response = urllib.request.urlopen(req) 
the_page = response.read() 

Le problème est ..

Traceback (most recent call last): 
    File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/requests/packages/urllib3/connectionpool.py", line 578, in urlopen 
    chunked=chunked) 
    File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/requests/packages/urllib3/connectionpool.py", line 362, in _make_request 
    conn.request(method, url, **httplib_request_kw) 
    File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 1083, in request 
    self._send_request(method, url, body, headers) 
    File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 1128, in _send_request 
    self.endheaders(body) 
    File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 1079, in endheaders 
    self._send_output(message_body) 
    File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 913, in _send_output 
    self.send(message_body) 
    File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 885, in send 
    self.sock.sendall(data) 
OSError: [Errno 22] Invalid argument 

Pourriez-vous me donner quelques conseils?

+0

connexes: [Python: HTTP Poster un grand fichier avec le streaming] (http://stackoverflow.com/q/2502596/4279) – jfs

+0

non apparentés: vous ne devriez pas charger le fichier en mémoire. Mais si vous utilisez alors 'data = f.read()' au lieu de 'data = b" ". Join (...)' – jfs

+0

Sans rapport: http://stackoverflow.com/questions/11662960/ioerror-errno- 22-invalid-argument-when-lecture-écriture-grosse-bytestring Je ne peux pas utiliser f.read() sur un gros fichier (environ <3GB) dans python3. Je ne connais pas la raison. Merci pour vos conseils connexes et sans rapport !! – Astin

Répondre

1
import requests 
url = 'http://domain.com/api/upload' 
with open('3GB.mov', 'br') as f: 
    for chunk in read_in_chunks(f): 

     offset = index + len(chunk) 
     headers['Content-Type'] = 'application/octet-stream' 
     headers['Content-length'] = content_size 
     headers['Content-Range'] = 'bytes %s-%s/%s' % (index, offset, content_size) 
     index = offset 
     try: 
      r = requests.post(url, data=chunk, headers=headers) 
      print "r: %s, Content-Range: %s" % (r, headers['Content-Range']) 
     except Exception, e: 
      print e 
+0

Vous pouvez vérifier req.get_method(), hm peut-être vous avez raison – Janom

+0

C'est une bonne idée. mais je ne pouvais pas utiliser la demande de distance dans mon serveur api. Merci pour vos conseils, Janom. – Astin