Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python和Urllib2上载文件时出现问题_Python_Urllib2 - Fatal编程技术网

使用python和Urllib2上载文件时出现问题

使用python和Urllib2上载文件时出现问题,python,urllib2,Python,Urllib2,以上代码用于流式传输和上载数据。流媒体的表现很好。上传时,代码挂起在下面的代码中 urllib2.urlopenrequest.read使用strace或Wireshark等网络嗅探器查找问题。一个showstopper可能是填充urllib2.Request的参数 import os import sys import time import base64 import hmac import mimetypes import urllib2 from hashlib import sha1

以上代码用于流式传输和上载数据。流媒体的表现很好。上传时,代码挂起在下面的代码中
urllib2.urlopenrequest.read

使用strace或Wireshark等网络嗅探器查找问题。

一个showstopper可能是填充urllib2.Request的参数

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()

upload_file('C:\\test.pdf', "http://10.105.158.132:26938/DocLib1/ste.pdf")

第一个参数应该是有效的URL。从您的共享代码来看,似乎bucket中已经填充了S3的URL。这将导致urlopen失败,因为它正在使用该调用的返回值。

该地址在“我的导航器”中生成连接超时。是否正确?您的缩进似乎不正确。打印“before”和urllib2.urlopen之间的行应缩进到upload_文件函数中。
request = urllib2.Request(bucket, data=data)