urllib.urlopen 不会在线程上执行

发布于 2024-10-13 08:35:06 字数 820 浏览 11 评论 0原文

我正在使用线程,我需要下载一个带有线程的网站。我还有一个线程将请愿书发送到该网站,但不等待答复。 不等待的情况是这样的:

class peticion(Thread):
    def __init__(self, url):
        Thread.__init__(self)
        self.url = url

    def run(self):
        f = urllib.urlopen(self.url)
        f.close()

这个工作正常,但是必须等待响应的情况需要随机时间才能完成,从 5 秒到 2 分钟,或者可能永远不会完成。这就是类:

class playerConn(Thread):
    def __init__(self, ev):
        Thread.__init__(self)
        self.ev = ev

    def run(self):
        try:
            params = urllib.urlencode('''params go here''')
            f = urllib.urlopen('''site goes here''')
            resp = f.read()
            f.close()
        finally:
            # do something with the response

无论我是否使用 try...finally 语句,它都不起作用,urlopen 函数后面的代码都不会执行。

我能做些什么?

I'm working with threads and I need to download a website with a thread. I also have a thread that sends the petition to the site but doesn't wait for an answer.
The one that doesn't wait is this:

class peticion(Thread):
    def __init__(self, url):
        Thread.__init__(self)
        self.url = url

    def run(self):
        f = urllib.urlopen(self.url)
        f.close()

This one works correctly, however the one that has to wait for the response takes something like a random time to complete, from 5 seconds to 2 minutes, or it may never finish. This is the class:

class playerConn(Thread):
    def __init__(self, ev):
        Thread.__init__(self)
        self.ev = ev

    def run(self):
        try:
            params = urllib.urlencode('''params go here''')
            f = urllib.urlopen('''site goes here''')
            resp = f.read()
            f.close()
        finally:
            # do something with the response

Whether or not I use the try...finally statement it doesn't work, the code after the urlopen function won't get to execute.

What can I do?

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

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

发布评论

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

评论(1

赤濁 2024-10-20 08:35:06

看来只是 URL 的问题,代码没有问题,而且似乎没有做错什么。
我敢打赌您在网站上遇到了某种类型的问题,可能是 404 或类似的问题。
尝试在本地主机中打开某些内容,只是为了测试。

It appear to just be a problem with the URL, the code is fine and it does not appear to do something wrong.
I bet you have some type of problem from the website, maybe a 404 or similar.
Try opening something in localhost, just to test.

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