1717import sys
1818import tarfile
1919import time
20+ import http .client
21+ from urllib .parse import urlparse
2022
2123from threading import Timer
2224from six .moves .urllib .request import Request , urlopen
3133def main ():
3234 parser = cli .Parser ()
3335 parser .add_optional_arguments (cli .Argument .OVA_PATH , cli .Argument .DATACENTER_NAME ,
34- cli .Argument .RESOURCE_POOL , cli .Argument .DATASTORE_NAME )
36+ cli .Argument .RESOURCE_POOL , cli .Argument .DATASTORE_NAME ,
37+ cli .Argument .HTTP_BLOCK_SIZE )
3538 args = parser .get_args ()
3639 si = service_instance .connect (args )
3740
@@ -50,7 +53,11 @@ def main():
5053 else :
5154 datastore = get_largest_free_ds (datacenter )
5255
53- ovf_handle = OvfHandler (args .ova_path )
56+ http_block_size = None
57+ if args .http_block_size :
58+ http_block_size = int (args .http_block_size )
59+
60+ ovf_handle = OvfHandler (args .ova_path , http_block_size )
5461
5562 ovf_manager = si .content .ovfManager
5663 # CreateImportSpecParams can specify many useful things such as
@@ -183,7 +190,7 @@ class OvfHandler(object):
183190 It processes the tarfile, matches disk keys to files and
184191 uploads the disks, while keeping the progress up to date for the lease.
185192 """
186- def __init__ (self , ovafile ):
193+ def __init__ (self , ovafile , http_block_size = None ):
187194 """
188195 Performs necessary initialization, opening the OVA file,
189196 processing the files and reading the embedded ovf file.
@@ -194,6 +201,10 @@ def __init__(self, ovafile):
194201 self .tarfile .getnames ()))[0 ]
195202 ovffile = self .tarfile .extractfile (ovffilename )
196203 self .descriptor = ovffile .read ().decode ()
204+ if http_block_size is None :
205+ self .http_block_size = 10485760 # 10MB by default
206+ else :
207+ self .http_block_size = http_block_size
197208
198209 def _create_file_handle (self , entry ):
199210 """
@@ -264,8 +275,23 @@ def upload_disk(self, file_item, lease, host):
264275 ssl_context = ssl ._create_unverified_context ()
265276 else :
266277 ssl_context = None
267- req = Request (url , ovffile , headers )
268- urlopen (req , context = ssl_context )
278+
279+ method = 'POST'
280+ if not device_url .disk :
281+ headers ['Overwrite' ] = 't'
282+ method = 'PUT'
283+
284+ p = urlparse (url )
285+
286+ if p .scheme == 'http' :
287+ conn = http .client .HTTPConnection (p .netloc ,
288+ blocksize = self .http_block_size )
289+ else :
290+ conn = http .client .HTTPSConnection (p .netloc , context = ssl_context ,
291+ blocksize = self .http_block_size )
292+ conn .request (method , p .path , body = ovffile , headers = headers )
293+ conn .close ()
294+
269295
270296 def start_timer (self ):
271297 """
0 commit comments