python tcp服务器centos7

python tcp服务器centos7,python,tcp,wireshark,tcpserver,Python,Tcp,Wireshark,Tcpserver,我已经用Python2.7编写了一个小型tcp服务器。代码如下。 它部署在Centos 7上: 开始时,我收到错误Errno:104-Peer关闭了套接字连接。在wireshark中,所以我打开了目标端口 打开端口:/etc/sysconfig/iptables添加规则: -输入-m state-state NEW-ptcp-dport 5555-j ACCEPT 然后使用sudo systemctl restart iptables重新启动 我试着重新启动等'仍然得到错误 在wireshark

我已经用Python2.7编写了一个小型tcp服务器。代码如下。 它部署在Centos 7上:

开始时,我收到错误Errno:104-Peer关闭了套接字连接。在wireshark中,所以我打开了目标端口 打开端口:/etc/sysconfig/iptables添加规则: -输入-m state-state NEW-ptcp-dport 5555-j ACCEPT 然后使用sudo systemctl restart iptables重新启动 我试着重新启动等'仍然得到错误 在wireshark中,打开端口后出现错误:

我的python脚本基于和,在日志中看不到任何内容。。好像连连接都没有完成

 def startServer(self,host,port):  
    s = socket.socket()
    try:
        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        s.bind((host,port))
    except  SocketError as e:
        print ("TS: SocketError connection error %s" % e)               
        sys.exit()

    # Tell the server socket file descriptor to destroy itself when this program ends.
    socketFlags = fcntl.fcntl(s.fileno(), fcntl.F_GETFD)
    socketFlags |= fcntl.FD_CLOEXEC
    fcntl.fcntl(s.fileno(), fcntl.F_SETFD, socketFlags)
    s.listen(128)     # Allow a queue of up to 128 requests (connections).
    s.setblocking(0) # Use asynchronous sockets.

    fdmap = {s.fileno():s}

    # Listen to socket events on the server socket defined by the above bind() call.
    epoll = select.epoll()
    epoll.register(s.fileno(), select.EPOLLIN)

    connections = {}
    requests = {}
    responses = {}
    address = {}

    frameinfo = getframeinfo(currentframe()) #PT_INFO: for debug only.. 
    while True:
        try:
            events = epoll.poll(10) #TODO: improve this ... 
        except: #TODO: improve this ... 
            pass
        for fd,event in events:
            if fd is s.fileno():
                # connection is a new socket object.
                # address is client IP address. The format of address depends on the address family of the socket (i.e., AF_INET).
                connection, address = s.accept()
                print "got connnection req from: %s" % str(address)
                # Set new socket-connection to non-blocking mode.
                connection.setblocking(0)
                # Listen for read events on the new socket-connection.
                epoll.register(connection.fileno(), select.EPOLLIN)
                connections[connection.fileno()] = connection
                requests[connection.fileno()] = b''

            else:
                if event & select.EPOLLIN:
                    try:

                        # requests[fd] = connections[fd].recv(1024)
                        while(1):
                            t_data = connections[fd].recv(1024)
                            logLine = ("TS: got data %s" % t_data)
                            data += t_data
                    except  SocketError as e:
                        if ex.errno == 11:  # EAGAIN
                            # Nothing more to read;
                            pass                            
                        continue

                    #do somthing with the data
                    print "got data:" + data

                # A socket was closed on our end.
                elif event & select.EPOLLHUP:
                    print "Closed connection to", connections[fd]['address']
                    epoll.unregister(fd)
                    del connections[fd]

                # Error on a connection.
                elif event & select.EPOLLERR:
                    print "Error on connection to", connections[fd]['address']
                    epoll.modify(fd, 0)
                    connections[fd]['connection'].shutdown(socket.SHUT_RDWR)
                else:
                    #test connnection is still no by writing to the socket
                    try:
                        # todo = 'write to the socket so it will give error'
                        print "sending ' '  to socket to test if it is still up"
                        connection.send(" ")
                    except:
                        print "after sending ' '  to socket got exception"
                        epoll.modify(fd, 0)
                        connections[fd]['connection'].shutdown(socket.SHUT_RDWR)

服务器套接字是在哪里创建的?谢谢你的重播,很抱歉我错过了。我更新了代码示例-服务器套接字是创建的第一件事。在selinux中打开端口有一点帮助:所有这些:semanage port-a-t http_port_t-p tcp 8088 semanage port-a-t http_port_t-p tcp 5555在centos 7中在/etc/sysconfig/iptables add下打开iptables中的端口:-a INPUT-m state-state NEW-p tcp-dport 8088-j ACCEPT-A INPUT-m state-state NEW-p tcp-dport 5555-j ACCEPT previus注释解决了部分问题:现在数据在大约2-3分钟的几次重试后到达之前的错误是:telnet:connect to address 192.168.1.104:发出时连接被拒绝:telnet 192.168.1.104 5555知道延迟的原因吗?创造了什么?[我相信这在我的python脚本中,但不知道我缺少了什么]