处理python套接字中的超时错误

处理python套接字中的超时错误

问题描述:

我试图弄清楚如何使用try并处理套接字超时。

I am trying to figure out how to use the try and except to handle a socket timeout.

from socket import *

def main():
    client_socket = socket(AF_INET,SOCK_DGRAM)
    client_socket.settimeout(1)
    server_host = 'localhost'
    server_port = 1234
    while(True):
        client_socket.sendto('Message',(server_host,server_port))
        try:
            reply, server_address_info = client_socket.recvfrom(1024)
            print reply
        except socket.Timeouterror:
            #more code

我添加套接字模块的方式是导入所有内容,但是我如何处理它说的文档中的异常您可以使用socket.timeouterror,但这对我不起作用。另外,如果我做了 import socket ,我怎么写try异常块?有人可以解释进口的差异吗?

The way I added the socket module was to import everything, but how do I handle exceptions in the docs it says you can use socket.timeouterror, but that doesn't work for me. Also, how would I write the try exception block if I did import socket? Can someone also explain the difference in the imports.

from foo import * 

在以下位置添加所有名称,但不带下划线(或仅在模块 __ all __ 属性中定义的名称) foo 到当前模块中。

adds all the names without leading underscores (or only the names defined in the modules __all__ attribute) in foo into your current module.

在上面的代码中,有 from socket import * 您只想捕获 timeout ,因为您已将 timeout 拉入当前名称空间。

In the above code with from socket import * you just want to catch timeout as you've pulled timeout into your current namespace.

从套接字导入* 提取 socket $内的所有内容的定义,但不会自己添加 socket

from socket import * pulls in the definitions of everything inside of socket but doesn't add socket itself.

try:
    # socketstuff
except timeout:
    print 'caught a timeout'

许多人认为进口* 有问题,并尝试避免这样做。这是因为在以这种方式导入的2个或更多模块中的公用变量名称将相互破坏。

Many people consider import * problematic and try to avoid it. This is because common variable names in 2 or more modules that are imported in this way will clobber one another.

例如,请考虑以下三个python文件:

For example, consider the following three python files:

# a.py
def foo():
    print "this is a's foo function"

# b.py
def foo():
    print "this is b's foo function"

# yourcode.py
from a import *
from b import *
foo()

如果运行 yourcode.py ,您只会看到输出这是b的foo函数。

If you run yourcode.py you'll see just the output "this is b's foo function".

由于这个原因,我建议要么导入模块并使用它,要么从模块中导入特定名称:

For this reason I'd suggest either importing the module and using it or importing specific names from the module:

例如,使用显式导入,您的代码将如下所示:

For example, your code would look like this with explicit imports:

import socket
from socket import AF_INET, SOCK_DGRAM

def main():
    client_socket = socket.socket(AF_INET, SOCK_DGRAM)
    client_socket.settimeout(1)
    server_host = 'localhost'
    server_port = 1234
    while(True):
        client_socket.sendto('Message', (server_host, server_port))
        try:
            reply, server_address_info = client_socket.recvfrom(1024)
            print reply
        except socket.timeout:
            #more code

只需多一点输入,但所有内容都是明确的,并且对于所有内容都来自读者来说是显而易见的。

Just a tiny bit more typing but everything's explicit and it's pretty obvious to the reader where everything comes from.