Trying to create a crude send/receive through TCP in python
<p>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.</p>

<p>SERVER CODE
</p>

<pre class="lang-py prettyprint-override"><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 &lt; 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()
</code></pre>

<p>CLIENT CODE</p>

<pre class="lang-py prettyprint-override"><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 -&gt; ")    

        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 &lt; 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()
</code></pre>

<p>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.</p>

<p>Please and thank you! </p>

<p>On the serverside, you're trying to use the same connection for your two threads t and u.</p>

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

<p>I always use the more high-level <code>socketserver</code> module (<a href="https://docs.python.org/3.6/library/socketserver.html#socketserver-tcpserver-example" rel="nofollow">Python Doc on socketserver</a>), which also natively supports Threading. I recommend checking it out!</p>

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

python tcp