简体   繁体   English

urllib2在python中发布超时

[英]urllib2 post timeout in python

I'm possting a file to a webserver using urllib2.py in my script and it keeps timining out. 我在脚本中使用urllib2.py将文件保存到Web服务器,并且该文件不断更新。 My code: 我的代码:

def postdata(nodemac,filename,timestamp):
    try:
        wakeup()
        socket.setdefaulttimeout(TIMEOUT)
        opener = urllib2.build_opener(MultipartPostHandler.MultipartPostHandler)
        host = HOST
        func = "post_data"
        url = "http://{0}{1}?f={2}&nodemac={3}&time={4}".format(host, URI, func, nodemac, timestamp)
        if os.path.isfile(filename):
            data = {"data":open(filename,"rb")}
            response = opener.open(url, data, timeout=TIMEOUT)
            retval = response.read()
            if "SUCCESS" in retval:
                return 0
            else:
                print "RETVAL "+retval
                return 99
        else:
            print filename +" is not a file"
            return 99
    except Exception as e:
        print e
        return 99

I set TIMEOUT to 20, 60 and 120 but the same thing keeps happening. 我将TIMEOUT设置为20、60和120,但是同一件事不断发生。 What's going on with that I'm wondering? 我想知道这是怎么回事? Something is foul! 犯规! The timeout set to 20 used to work just fine and then all of a sudden, today it started to time out on me... does anyone have any clues? 设置为20的超时过去可以正常工作,然后突然之间,今天它开始对我超时...有人有任何线索吗? I couldn't find anything that brought me further on the web so I thought I'd try here...! 我找不到任何使我在网络上走得更远的东西,所以我想我可以在这里尝试!

Thanks, 谢谢,

Traceback: 追溯:

 File "gateway.py", line 686, in CloudRun
    read_img()
  File "gateway.py", line 668, in read_img
    retval = database.postimg(mac,fh,timestmp)
  File "/root/database.py", line 100, in postimg
    response = opener.open(url, data, timeout=TIMEOUT)
  File "/usr/lib/python2.7/urllib2.py", line 394, in open
  File "/usr/lib/python2.7/urllib2.py", line 412, in _open
  File "/usr/lib/python2.7/urllib2.py", line 372, in _call_chain
  File "/usr/lib/python2.7/urllib2.py", line 1199, in http_open
  File "/usr/lib/python2.7/urllib2.py", line 1174, in do_open
URLError: <urlopen error timed out>

The timeout set to 20 used to work just fine and then all of a sudden, today it started to time out on me... does anyone have any clues? 设置为20的超时过去可以正常工作,然后突然之间,今天它开始对我超时...有人有任何线索吗?

Well, the first clue is that the behavior changed even though your code didn't. 好吧,第一个线索是即使您的代码没有更改,行为也会改变。 So, it's got to be something with the environment. 因此,必须与环境有关。

The most likely possibility is that the timeouts are a perfectly accurate sign that the server is overloaded, broken, etc. But, since you gave us no information at all about the server you're trying to talk to, it's hard to do much more than guess. 最有可能的可能性是超时是服务器过载,损坏等的完全准确的信号。但是,由于您根本没有提供与您要与之交谈的服务器的任何信息,因此很难做更多的事情比猜测。

Here are some ideas for tracking down the problem. 这里有一些解决问题的方法。

First, take that exact same URL, paste it into your browser's address bar, and see what happens. 首先,使用完全相同的URL,将其粘贴到浏览器的地址栏中,然后看看会发生什么。 Does it time out? 会超时吗? Or take longer than 20 seconds to respond? 还是需要超过20秒的时间才能做出回应?

Try adding &data=data to the end of the URL. 尝试将&data=data添加到URL的末尾。

Try using a simple tool to send the request as a POST. 尝试使用简单的工具以POST形式发送请求。 For example, from the command line, try curl -d data http://whatever . 例如,从命令行尝试curl -d data http://whatever

Try logging exactly what headers and post data urllib2 used, and making curl send exactly the same thing. 尝试准确记录使用的头文件和发布数据urllib2 ,并使curl发送完全相同的东西。 (If this fails, and the previous one worked, you may want to edit your question, or ask a new question, with "Why did this work and that fail?" with the details.) (如果失败,并且上一个可行,则可能要编辑您的问题或提出一个新问题,并带有“为什么这样做会失败?”的详细信息。)

Try running the same tests from a different computer with a different internet connection. 尝试从具有不同Internet连接的另一台计算机上运行相同的测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM