尝试打开,写入和关闭套接字几次时连接被拒绝(Python)

时间:2020-12-28 15:45:53

I have a program that listens on a port waiting for a small amount of data to tell it what to do. I run 5 instances of that program, one on each port from 5000 to 5004 inclusively.

我有一个程序监听端口等待少量数据告诉它该做什么。我运行该程序的5个实例,每个端口一个包含5000到5004。

I have a second program written in Python that creates a socket "s", writes the data to port 5000, then closes. It then increments the port number and creates a new socket connected to 5001. It does this until it reaches 5004.

我有一个用Python编写的第二个程序,它创建一个套接字“s”,将数据写入端口5000,然后关闭。然后它递增端口号并创建一个连接到5001的新套接字。它会一直到达5004。

The first three socket connections work just fine. The programs on ports 5000 to 5002 receive the data just fine and off they go! However, the fourth socket connection results in a "Connection Refused" error message. Meanwhile, the fourth listening program still reports that it is waiting on port 5003 -- I know that it's listening properly because I am able to connect to it using Telnet.

前三个套接字连接工作得很好。端口5000到5002上的程序可以很好地接收数据,然后关闭它们!但是,第四个套接字连接导致“连接被拒绝”错误消息。同时,第四个监听程序仍然报告它正在等待端口5003 - 我知道它正在正常收听,因为我可以使用Telnet连接到它。

I've tried changing the port numbers, and each time it's the fourth connection that fails.

我已经尝试更改端口号,每次都是第四次连接失败。

Is there some limit in the system I should check on? I only have one socket open at any time on the sending side, and the fact that I can get 3 of the 5 through eliminates a lot of the basic troubleshooting steps I can think of.

我应该检查的系统是否有一些限制?我只在发送端随时打开一个插槽,而且我可以获得5个中的3个,这消除了我能想到的许多基本故障排除步骤。

Any thoughts? Thanks!

有什么想法吗?谢谢!

--- EDIT ---

---编辑---

I'm on CentOS Linux with Python version 2.6. Here's some example code:

我在使用Python 2.6版的CentOS Linux上。这是一些示例代码:

try:
        portno = 5000
        for index, data in enumerate(data_list):
                s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                s.connect(('myhostname', portno))
                s.sendall(data)
                s.close()
                portno = portno + 1
except socket.error, (value,message):
        print 'Error: Send could not be performed.\nPort: ' + str(portno) + '\nMessage: ' + message
        sys.exit(2)

3 个解决方案

#1


This sounds a lot like the anti-portscan measure of your firewall kicking in.

这听起来很像你的防火墙的反端口扫描措施。

#2


I don't know that much about sockets, so this may be really bad style... use at own risk. This code:

我不太了解套接字,所以这可能是非常糟糕的风格......使用风险自负。这段代码:

#!/usr/bin/python
import threading, time
from socket import *
portrange = range(10000,10005)

class Sock(threading.Thread):
    def __init__(self, port):
        self.port = port
        threading.Thread.__init__ ( self )

    def start(self):
        self.s = socket(AF_INET, SOCK_STREAM)
        self.s.bind(("localhost", self.port))
        self.s.listen(1)
        print "listening on port %i"%self.port
        threading.Thread.start(self)
    def run(self):
        # wait for client to connect
        connection, address = self.s.accept()
        data = True
        while data:
            data = connection.recv(1024)
            if data:
                connection.send('echo %s'%(data))
        connection.close()

socketHandles = [Sock(port) for port in portrange]

for sock in socketHandles:
    sock.start()

# time.sleep(0.5)

for port in portrange:
    print 'sending "ping" to port %i'%port
    s = socket(AF_INET, SOCK_STREAM) 
    s.connect(("localhost", port))
    s.send('ping')
    data = s.recv(1024)
    print 'reply was: %s'%data
    s.close()

should output:

listening on port 10000
listening on port 10001
listening on port 10002
listening on port 10003
listening on port 10004
sending "ping" to port 10000
reply was: echo ping
sending "ping" to port 10001
reply was: echo ping
sending "ping" to port 10002
reply was: echo ping
sending "ping" to port 10003
reply was: echo ping
sending "ping" to port 10004
reply was: echo ping

perhaps this will help you see if it's your firewall causing troubles. I suppose you could try splitting this into client and server as well.

也许这会帮助你看看你的防火墙是否会造成麻烦。我想你可以尝试将它分成客户端和服务器。

#3


Are you running the 2nd Python program from within Idle? If so - try it outside of Idle and see if the results are any different.

你是否在Idle中运行第二个Python程序?如果是这样 - 在空闲之外尝试它,看看结果是否有所不同。

#1


This sounds a lot like the anti-portscan measure of your firewall kicking in.

这听起来很像你的防火墙的反端口扫描措施。

#2


I don't know that much about sockets, so this may be really bad style... use at own risk. This code:

我不太了解套接字,所以这可能是非常糟糕的风格......使用风险自负。这段代码:

#!/usr/bin/python
import threading, time
from socket import *
portrange = range(10000,10005)

class Sock(threading.Thread):
    def __init__(self, port):
        self.port = port
        threading.Thread.__init__ ( self )

    def start(self):
        self.s = socket(AF_INET, SOCK_STREAM)
        self.s.bind(("localhost", self.port))
        self.s.listen(1)
        print "listening on port %i"%self.port
        threading.Thread.start(self)
    def run(self):
        # wait for client to connect
        connection, address = self.s.accept()
        data = True
        while data:
            data = connection.recv(1024)
            if data:
                connection.send('echo %s'%(data))
        connection.close()

socketHandles = [Sock(port) for port in portrange]

for sock in socketHandles:
    sock.start()

# time.sleep(0.5)

for port in portrange:
    print 'sending "ping" to port %i'%port
    s = socket(AF_INET, SOCK_STREAM) 
    s.connect(("localhost", port))
    s.send('ping')
    data = s.recv(1024)
    print 'reply was: %s'%data
    s.close()

should output:

listening on port 10000
listening on port 10001
listening on port 10002
listening on port 10003
listening on port 10004
sending "ping" to port 10000
reply was: echo ping
sending "ping" to port 10001
reply was: echo ping
sending "ping" to port 10002
reply was: echo ping
sending "ping" to port 10003
reply was: echo ping
sending "ping" to port 10004
reply was: echo ping

perhaps this will help you see if it's your firewall causing troubles. I suppose you could try splitting this into client and server as well.

也许这会帮助你看看你的防火墙是否会造成麻烦。我想你可以尝试将它分成客户端和服务器。

#3


Are you running the 2nd Python program from within Idle? If so - try it outside of Idle and see if the results are any different.

你是否在Idle中运行第二个Python程序?如果是这样 - 在空闲之外尝试它,看看结果是否有所不同。