Search code examples
pythontcpclientfile-writing

Receiving data from server and writing it into a .txt file using TCP socket programming in python


I am receiving data blocks from a server that sends out the data block as a string with the first 3 bytes representing the number of bytes of the data. The rest of the data are separated by tabs between them. I want to receive them in my client and write it to the text file. But when I write it to the .txt file, the previous data is getting re-written. Also, I want to limit the number of data blocks being received. Here is my code:

Server.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()

Client.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()


Solution

  • Open your file in append mode:

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