Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Make a Server and Client with Python Socket

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 401
    Answer it

    I am trying to make a server and client which sends a file from client to server and the server saves it to hard then the server asks for another file and if the answer of the client is yes then the client sends the second file then the server again saves it and if the client answer is no server closes the socket when i run this code the first file is sent and received successfully but after that both of the server and the client freeze and nothing happens what is wrong with it and how can i fix it please correct my code?

     

    My server code:

     

    import socket
    
    host = 'localhost'
    
    port = 4444
    
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    
    s.bind((host, port))
    
    s.listen(5) 
    
    (client, (ip, port))=s.accept()
    
    
    while True:
    
        data = "".join(iter(lambda: client.recv(1), "\n"))
    
    with open('filehere.txt', 'w') as file:
    
    
    
        for item in data:
    
            file.write("%s" % item)
    
        if not data: break
    
    
    client.send("is there any other file?")
    
    d = client.recv(2048)
    
    if d == "yes":
    
        while True:
    
            data = "".join(iter(lambda: client.recv(1), "\n")
    
            with open('filehere1.txt', 'w') as file:
    
                 for item in data:
    
                     file.write("%s" % item)
    
            if not data: break
    
            s.close()
    
    else:
    
        s.close()

     

    my client code: 

     

    import socket
    
    host = 'locahost'
    
    port = 4444
    
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    
    s.connect((host, port))
    
    
    f = open('myfile.txt', 'rb')
    
    l = f.read()
    
    while True:
    
        for line in l:
    
            s.send(line)
    
        break
    
    f.close()
    
    d = s.recv(2048)
    
    a = raw_input(d)
    
    
    if a == "yes":
    
        s.send("yes")
    
        f = open('myfile1', 'rb')
    
        l = f.read()
    
        while True:
    
            for line in l:
    
                s.send(line)
            break
    
        f.close()
    
    else:
    
        s.close()

 0 Answer(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: