socket python中的异常列表

时间:2016-11-25 06:48:16

标签: python exception-handling tcpsocket

在Python语言中使用socket lib的函数时会产生什么异常。 我有异常错误列表,但不知道socket函数有哪些错误。 https://docs.python.org/2/library/errno.html 我想处理TCP套接字中的每个错误情况。

import socket
import sys
sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ('192.168.1.26', 56000)
while True:
    try:
        sock.connect(server_address)
        break
    except socket.error as e:
        print "error while connecting ::%s",e
while True:
    try:

        # Send data
        message = 'This is the message.  It will be repeated.'
        print >>sys.stderr, 'sending "%s"' % message
        sock.sendall(message)
    except socket.error as e:
        print "error while sending ::%s",e

1 个答案:

答案 0 :(得分:2)

因为你发送了很多请求 当你发送许多请求 主持人会自动阻止你 我将尝试用我自己的脚本修复重复错误异常:

import socket
import sys
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server = '192.168.1.26'
port = 56000
while True:
    try:
        s.connect((server, port))
    except socket.error as e:
        print "error while connecting :: %s" % e
        break
while True:
    try:
        # Send data
        message = 'This is the message.  It will be repeated.'
        print 'sending "%s"' % message
        s.send(message)
    except socket.error as e:
        print "error while sending :: " + str(e)
        break

多数民众赞成