首页 > 解决方案 > 使用python中的TCP套接字编程从服务器接收数据并将其写入.txt文件

问题描述

我从服务器接收数据块,该服务器将数据块作为字符串发送出去,前 3 个字节表示数据的字节数。其余数据由它们之间的制表符分隔。我想在我的客户端接收它们并将其写入文本文件。但是当我将它写入 .txt 文件时,之前的数据会被重新写入。另外,我想限制接收到的数据块的数量。 这是我的代码:

服务器.py

def Main():
    host = '127.0.0.1'
    port = 5003
    s = socket.socket()
    s.bind((host,port))
    s.listen(5)
    print("Server started")

    while True:
        c,addr = s.accept()
        print("Client connected ip:<" + str(addr) + ">")
        #c.sendall('232 23/02/2020  18:11:56    567 4   1   6   0   0   0   0   1510.26 1524.211    1536.369    1550.656    1563.790    1577.245    45  58  41  53  47  52  2   10  0   0   0   0   1510.83 1518.992    1526.89 1534.056    1542.597    1550.394    1558.07 1566.34 1574.286    1582.056    44  60  42  41  47  48  42  44  42  49  3   7   0   0   0   0   1510.95 1518.936    1528.055    1536.916    1545.605    1555.107    1563.437    1572.349    1582.091    59  41  46  45  41  53  42  43  60  4   15  0   0   0   0   1510.72 1516    1520.677    1526.604    1531.569    1537.594    1542.333    1547.744    1553.187    1558.118    1564.245    1569.357    1574.308    1579.758    1585.661    54  53  40  52  45  49  47  48  50  42  58  54  46  48  60'.encode())   
        # t = threading.Thread(target = RetrFile, args=("retrThread", c,))
        #c.sendall('232 24/02/2020  18:11:56    567 4   1   6   0   0   0   0   1510.26 1524.211    1536.369    1550.656    1563.790    1577.245    45  58  41  53  47  52  2   10  0   0   0   0   1510.83 1518.992    1526.89 1534.056    1542.597    1550.394    1558.07 1566.34 1574.286    1582.056    44  60  42  41  47  48  42  44  42  49  3   7   0   0   0   0   1510.95 1518.936    1528.055    1536.916    1545.605    1555.107    1563.437    1572.349    1582.091    59  41  46  45  41  53  42  43  60  4   15  0   0   0   0   1510.72 1516    1520.677    1526.604    1531.569    1537.594    1542.333    1547.744    1553.187    1558.118    1564.245    1569.357    1574.308    1579.758    1585.661    54  53  40  52  45  49  47  48  50  42  58  54  46  48  60'.encode())   
        #c.sendall('\t')
        # t.start()
        c.sendall('67 24/02/2020    18:11:56    567 4   1   6   0   0   0   0   1510.26 1524.211    1536.369    155'.encode())
        c.sendall('67 25/02/2020    18:11:56    567 4   1   6   0   0   0   0   1510.26 1524.211    1536.369    156'.encode())
        c.close()

    # s.close()

if __name__ == '__main__':
    Main()

客户端.py

def Main():
    host = '127.0.0.1'
    port = 5003
    s = socket.socket()
    s.connect((host,port))

    i = 0
    #for i < 2:
    len_message = s.recv(3)
    print(len_message)
    while len_message:
        bytes_length = int(len_message.decode())
        #data_length = (bytes_length + 3)
        print(bytes_length)
        #print(data_length)
        data = s.recv(bytes_length)
        print(data)
        write_file(data)
        len_message = s.recv(3)
    #i+=1
    s.close()

def write_file(data):
        with open("Output.txt", "wb") as text_file:
            text_file.write(data)
            text_file.write('\n'.encode())


if __name__ == '__main__':
    Main()

标签: pythontcpclientfile-writing

解决方案


以附加模式打开文件:

with open("Output.txt", "ab") as text_file:

推荐阅读