阅读背景:

Python:如何区分套接字错误和超时?

来源:互联网 

I'm having the following code:

我有以下代码:

try:
    while 1:
        s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        s.settimeout(5);
        s.connect((HOST,PORT))
        print("before send")
        #time.sleep(10);
        #s.sendall('GET / HTTP/1.1\r\nConnection: Keep-Alive\r\nHost: www.google.lt\r\n\r\n')
        data=s.recv(52)
        print("after send");
        s.close()
        if string.find(data,"HTTP/1.1 200 OK") == -1:
            print("Lost Connection")
        print(data)
        time.sleep(2)
except KeyboardInterrupt:
    print("CTRL C occured")
except socket.error:
    print("socket error occured: ")
except socket.timeout:
    print("timeout error")
try:



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: