Trying to create a crude send/receive through TCP in python
Question: So far I can send files to my "fileserver" and retrieve files from there as
well. But i can't do both at the same time. I have to comment out one of the
other threads for them to work. As you will see in my code.

SERVER CODE

    
    
    from socket import *
    import threading
    import os
    
    # Send file function
    def SendFile (name, sock):
    filename = sock.recv(1024)      
      if os.path.isfile(filename):    
        sock.send("EXISTS " + str(os.path.getsize(filename))) 
        userResponse = sock.recv(1024)      
        if userResponse[:2] == 'OK':        
            with open(filename, 'rb') as f:
                bytesToSend = f.read(1024)
                sock.send(bytesToSend)
                while bytesToSend != "":   
                    bytesToSend = f.read(1024)
                    sock.send(bytesToSend)
    else:                                   
        sock.send('ERROR')
    
    sock.close()
    
    def RetrFile (name, sock):
      filename = sock.recv(1024)      
      data = sock.recv(1024)                     
         if data[:6] == 'EXISTS':            
           filesize = long(data[6:])       
           sock.send('OK')
           f = open('new_' + filename, 'wb')      
           data = sock.recv(1024)
           totalRecieved = len(data)               
           f.write(data)
           while totalRecieved < filesize:         
             data = sock.recv(1024)
             totalRecieved += len(data)
             f.write(data)
    
    sock.close()
    
    
    
    myHost = ''                             
    myPort = 7005                           
    
    s = socket(AF_INET, SOCK_STREAM)     
    s.bind((myHost, myPort))             
    
    s.listen(5)
    
    print("Server Started.")
    
    while True:                             
    
    connection, address = s.accept()
    print("Client Connection at:", address)
    
    #    u = threading.Thread(target=RetrFile, args=("retrThread", connection))
    t = threading.Thread(target=SendFile, args=("sendThread", connection))     
    #    u.start()
    t.start()
    
    
    s.close()
    

CLIENT CODE

    
    
    from socket import *
    import sys
    import os
    
    servHost = ''                          
    servPort = 7005                         
    
    s = socket(AF_INET, SOCK_STREAM)        
    s.connect((servHost, servPort))         
    
    decision = raw_input("do you want to send or retrieve a file?(send/retrieve): ")
    
    if decision == "retrieve" or decision == "Retrieve":
      filename = raw_input("Filename of file you want to retrieve from server: ")      # ask user for filename
      if filename != "q":                     
         s.send(filename)                    
         data = s.recv(1024)                 
         if data[:6] == 'EXISTS':           
            filesize = long(data[6:])       
            message = raw_input("File Exists, " + str(filesize)+"Bytes, download?: Y/N -> ")    
    
            if message == "Y" or message == "y":
                s.send('OK')
                f = open('new_' + filename, 'wb')       
                data = s.recv(1024)                     
                totalRecieved = len(data)               
                f.write(data)
                while totalRecieved < filesize:         
                    data = s.recv(1024)
                    totalRecieved += len(data)
                    f.write(data)
                    print("{0: .2f}".format((totalRecieved/float(filesize))*100)) + "% Done" # print % of download progress
    
                print("Download Done!")
    
        else:
            print("File does not exist!")
    s.close()
    
    elif decision == "send" or decision == "Send":
    filename = raw_input("Filename of file you want to send to server: ")
    if filename != "q":
        s.send(filename)                    
        if os.path.isfile(filename):    
            s.send("EXISTS " + str(os.path.getsize(filename))) 
            userResponse = s.recv(1024)      
            if userResponse[:2] == 'OK':
                with open(filename, 'rb') as f: 
                    bytesToSend = f.read(1024)
                    s.send(bytesToSend)
                    while bytesToSend != "":    
                        bytesToSend = f.read(1024)
                        s.send(bytesToSend)
        else:                                   
            s.send('ERROR')
    
    s.close()
    
    
    s.close()
    

I'm still new to programming, so this is quite tough for me. All in all i'm
just trying to figure out how to send AND receive files without having to
comment out the bottom threads in my SERVER CODE.

Please and thank you!


Answer: On the serverside, you're trying to use the same connection for your two
threads t and u.

I think it might work if you listened for another connection in your `while
True:` loop on the server, after you started your first thread.

I always use the more high-level `socketserver` module ([Python Doc on
socketserver](https://docs.python.org/3.6/library/socketserver.html#socketserver-
tcpserver-example)), which also natively supports Threading. I recommend
checking it out!

By the way, since you do a lot of `if (x == 'r' or x == 'R')`: you could just
do `if x.lower() == 'r'`

