Python client server to send a file Beginner Error
Question: I am trying to do simple code to send file from the client to the server after
saving in t some data. I am a beginner so I can't figure where the problem is
or what is the missing function or line in my code

The Server :

    
    
    import socket
    
    server_socket = socket.socket()
    server_socket.bind(('0.0.0.0', 8000))
    server_socket.listen(0)
    BUFFER_SIZE = 1024
    conn, addr = server_socket.accept()
    print ('Got connection from', addr)
    while 1:
        data = conn.recv(BUFFER_SIZE)
        if not data: 
            break
        fileREC=open (data , 'rb')
    

The Client

    
    
    import socket
    client_socket = socket.socket()
    client_socket.connect(("192.168.1.4", 8000))
    BUFFER_SIZE = 1024
    TextFile= open ("TextFile","w")
    TextFile.write("Here is the file")
    TextFile.write("Writing data")
    TextFile.close()
    f=open (TextFile , 'wb')
    print ("Writing the file to binart ")
    client_socket .send(f)
    print ("Data Sent")
    

The Error

    
    
      ERROR:Traceback (most recent call last):
      File "tenmay.py", line 5, in <module>
       client_socket.connect(("192.168.1.4", 8000))
      File "/usr/lib/python2.7/socket.py", line 224, in meth
      return getattr(self._sock,name)(*args)
      socket.error: [Errno 111] Connection refused
    


Answer: Send the contents of the file not the filehandle:

    
    
    f=open ("TextFile", 'rb')
    client_socket.send(f.read())
    

The second time the client runs the server is waiting to recv data because the
accept() command is outside of the loop.

The client could repeatedly send data from a loop, but not if the program ends
and has to be restarted.

