python上传文件内容需要很长时间

发布于 2024-10-25 08:46:37 字数 1021 浏览 7 评论 0原文

我正在尝试使用以下代码上传文件:

url = "/folder/sub/interface?"
connection = httplib.HTTPConnection('www.mydomain.com')

def sendUpload(self):
    fields = []
    file1 = ['file1', '/home/me/Desktop/sometextfile.txt']
    f = open(file1[1], 'r')
    file1.append(f.read())
    files = [file1]
    content_type, body = self.encode_multipart_formdata(fields, files)

    myheaders['content-type'] = content_type
    myheaders['content-length'] = str(len(body))

    upload_data = urllib.urlencode({'command':'upload'})
    self.connection.request("POST", self.url + upload_data, {}, myheaders)
    response = self.connection.getresponse()
    if response.status == 200:
        data = response.read()
        self.connection.close()
        print data

encode_multipart_formdata() 来自 http://code。 activestate.com/recipes/146306/

当我执行该方法时,需要很长时间才能完成。事实上,我不认为它会结束..在网络监视器上我看到数据已传输,但该方法没有结束...

这是为什么?我应该在某处设置超时吗?

I'm trying to upload files with the following code:

url = "/folder/sub/interface?"
connection = httplib.HTTPConnection('www.mydomain.com')

def sendUpload(self):
    fields = []
    file1 = ['file1', '/home/me/Desktop/sometextfile.txt']
    f = open(file1[1], 'r')
    file1.append(f.read())
    files = [file1]
    content_type, body = self.encode_multipart_formdata(fields, files)

    myheaders['content-type'] = content_type
    myheaders['content-length'] = str(len(body))

    upload_data = urllib.urlencode({'command':'upload'})
    self.connection.request("POST", self.url + upload_data, {}, myheaders)
    response = self.connection.getresponse()
    if response.status == 200:
        data = response.read()
        self.connection.close()
        print data

The encode_multipart_formdata() comes from http://code.activestate.com/recipes/146306/

When I execute the method it takes a long time to complete. In fact, I don't think it will end.. On the network monitor I see that data is transferred, but the method doesn't end...

Why is that? Should I set a timeout somewhere?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

余生再见 2024-11-01 08:46:37

您似乎没有将请求的正文发送到服务器,因此它可能会陷入等待 content-length 字节到达的状态,而他们永远不会这样做。

您确定

self.connection.request("POST", self.url + upload_data, {}, myheaders)

不应该阅读吗

self.connection.request("POST", self.url + upload_data, body, myheaders)

You don't seem to be sending the body of your request to the server, so it's probably stuck waiting for content-length bytes to arrive, which they never do.

Are you sure that

self.connection.request("POST", self.url + upload_data, {}, myheaders)

shouldn't read

self.connection.request("POST", self.url + upload_data, body, myheaders)

?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文