Skip to content Skip to sidebar Skip to footer

Python Client Server Transfer .txt Not Writing To File

I'm trying to write a simple client/server in Python using a TCP socket but I can't seem to figure out why the file is not transferring. Client: import socket HOST = ''

Solution 1:

You are calling accept inside the while-loop. So you have only one recv-call that receives data, so break is never called.

Btw. you should use sendall, that guarantees, that all data is sent.

Client:

import socket

HOST = ''#server name goes in here
PORT = 3820             
socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.connect((HOST,PORT))
withopen('myUpload.txt', 'rb') as file_to_send:
    for data in file_to_send:
        socket.sendall(data)
print'end'
socket.close()

Server:

import socket
HOST = ''                 
PORT = 3820
socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.bind((HOST, PORT))
socket.listen(1)
conn, addr = socket.accept()
with open('myTransfer.txt', 'wb') as file_to_write:
    while True:
        data = conn.recv(1024)
        print dataif not data:
            break
        file_to_write.write(data)
socket.close()

Post a Comment for "Python Client Server Transfer .txt Not Writing To File"