Zdravim,<br><br>resim uz pekelne dlouho problem, jak z Pythonu uploadovat velky soubor pres PUT/POST - pokud mozno nejakym standardnim zpusobem.<br><br>Problem jsem zatim vyresil pomoci mmap. Nelibi se mi ale, ze na 32bit systemech je maximalni velikost uploadovaneho souboru ~4GB. Zaroven jsem ale neprisel na to, jak donutit urllib2, aby s daty pracovala jako se streamem (file-like objektem) a ne jako se stringem. Posledni napad je napsat si 'string-like object', ktery bude doopravdy soubor a nemapovat cely jeho obsah na pamet (jako dela mmap), ale seekovat a nejak to cist do bufferu. Prijde mi ale dost divne, ze by neexistovalo jednodussi reseni.<br>
<br>Soucasna implementace je nasledujici:<br><br> <span style="font-family: courier new,monospace;">from urllib2 import Request, urlopen</span><br><br> <span style="font-family: courier new,monospace;"> @classmethod</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> def _f2b(cls, f, size=None):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> '''</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> Map files into memory and act as buffer.</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> '''</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> if isinstance(f, basestring):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> # No conversion needed</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> return f</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> elif getattr(f, 'fileno', None):</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> # Map to memory</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> if size == None:</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> # When size is already known, skip this </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> f.seek(0, os.SEEK_END)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> size = f.tell()</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> f.seek(0)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> return mmap.mmap(f.fileno(), size)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> else:</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> raise fs.errors.UnsupportedError("Cannot handle type %s" % type(f))</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">@classmethod</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> def put(cls, path, data, size=None):</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> class PutRequest(Request):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> def __init__(self, *args, **kwargs):</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> self.get_method = lambda: 'PUT'</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> Request.__init__(self, *args, **kwargs)</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> </span><span style="font-family: courier new,monospace;"> data = cls._f2b(data, size=size)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> req = PutRequest(''.join([cls.webapi, path]), data, headers=cls.headers)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> return urlopen(req)</span><br>
<br><br>Diky za kazdou radu.<br><br>Marek