持续监听TCP端口

时间:2017-11-15 10:14:13

标签: python tcp

我已经制作了一个能够通过TCP协议从端口接收数据的代码。我每隔15分钟从ESP8266接收数据,然后ESP进入深度睡眠模式。如何更改它以使其连续工作?我想在while循环中创建一个新连接,但它不起作用。

我的代码

import sys
import socket

TCP_IP = '192.168.42.1'
TCP_PORT = 8888
BUFFER_SIZE = 1024
param = []
i=0

#s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#s.bind((TCP_IP,TCP_PORT))
#s.listen(1)

#print 'Listening for client...'

#conn, addr = s.accept()
#print 'Connection address:', addr
while 1:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((TCP_IP,TCP_PORT))
    s.listen(1)

    print 'Listening for client...'
    conn, addr = s.accept()
    print 'Connection address:', addr
    data = conn.recv(BUFFER_SIZE)
    if data == ";" :
            conn.close()
            print "Received all the data"
            i=0
            for x in param:
                    print x
            #break
    elif data:
            print "received data: ", data
            param.insert(i,data)
            i+=1
            #print "End of transmission"

编辑:

修改后的代码。

import sys
import socket

TCP_IP = '192.168.42.1'
TCP_PORT = 8888
BUFFER_SIZE = 1024
param = []
i=0

#s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#s.bind((TCP_IP,TCP_PORT))
#s.listen(1)

#print 'Listening for client...'

#conn, addr = s.accept()
#print 'Connection address:', addr
while 1:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((TCP_IP,TCP_PORT))
    s.listen(1)
    while 1: 
            print 'Listening for client...'
            conn, addr = s.accept()
            print 'Connection address:', addr
            data = conn.recv(BUFFER_SIZE)
            if data == ";" :
                    conn.close()
                    print "Received all the data"
                    i=0
                    for x in param:
                            print x
                    #break
            elif data:
                    print "received data: ", data
                    param.insert(i,data)
                    i+=1
                    #print "End of transmission"
    s.close()

我创建了第二个while循环。我现在可以连续听,但我只从ESP收到一个数据包(ESP发送9个数据包)。如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

如果要持续侦听来自远程端的连接和数据,可以使用select()

来实现此目的。

使用select()的代码的修改版本如下所示。这也将处理关闭连接的远程端:

import sys
import socket
import select

TCP_IP = '127.0.0.1'
TCP_PORT = 8888
BUFFER_SIZE = 1024
param = []

print 'Listening for client...'
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((TCP_IP,TCP_PORT))
server.listen(1)
rxset = [server]
txset = []

while 1:
    rxfds, txfds, exfds = select.select(rxset, txset, rxset)
    for sock in rxfds:
        if sock is server:
            conn, addr = server.accept()
            conn.setblocking(0)
            rxset.append(conn)
            print 'Connection from address:', addr
        else:
            try:
                data = sock.recv(BUFFER_SIZE)
                if data == ";" :
                    print "Received all the data"
                    for x in param:
                        print x
                    param = []
                    rxset.remove(sock)
                    sock.close()
                else:
                    print "received data: ", data
                    param.append(data)
            except:
                print "Connection closed by remote end"
                param = []
                rxset.remove(sock)
                sock.close()

注意我已经用环回替换了你的IP地址,但你明白了。

希望这可能有所帮助。