Problem uploading file using python and Urllib2
import os
import sys
import time
import base64
import hmac
import mimetypes
import urllib2
from hashlib import sha1
from poster.streaminghttp import register_openers
def read_data(file_object):
while True:
r = file_object.read(1 * 1024)
print 'rrr',r
if not r:
print 'r'
file_object.close()
break
yield r
def upload_file(filename, bucket):
print 'start'
length = os.stat(filename).st_size
content_type = mimetypes.guess_type(filename)[0]
date = time.strftime("%a, %d %b %Y %X GMT", time.gmtime())
print 'before'
register_openers()
print 'after'
input_file = open(filename, 'r')
print 'read mode'
data = read_data(input_file)
request = urllib2.Request(bucket, data=data)
request.add_header('Date', date)
request.add_header('Content-Type', content_type)
request.add_header('Content-Length', length)
request.get_method = lambda: 'PUT'
print 'before lamda'
urllib2.urlopen(request).read()
uplo开发者_如何学编程ad_file('C:\\test.pdf', "http://10.105.158.132:26938/DocLib1/ste.pdf")
the above code is for streaming and uploading the data. streaming is performing fine. While uploading, code hangs in following code urllib2.urlopen(request).read()
One showstopper may be your arguments populating the urllib2.Request
request = urllib2.Request(bucket, data=data)
The first argument should be a valid URL. From your shared code it doesn't appeared bucket has been populated with the URL to S3 for wherever. That would cause urlopen to fail as it is using the returned value for that call.
Use strace or a network sniffer like Wireshark for hunting the problem down.
精彩评论