#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    20
    Rep Power
    0

    Server and Client help


    Hello all,
    I'm having an issue with sending a file. sending the file actually works but it only stores it as a 4KB file... Any help would be greatly appreciated

    client
    Code:
    import socket
    import sys
    
    location_name = input("Insert file.... \n")
    if location_name[0] == '\''
        location_name = location_name[1:]
        while location_name[-1] != '\''
            location_name = location_name[0:-1]
        location_name = location_name[0:-1]
    
    
    try:
        file = open(location_name, 'rb')
        data = file.read(4096)
        file.close()
    except:
        print("Error loading file... \n")
        sys.exit()
    
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect((192.168.1.119, 8989))
        s.sendall(data)
        reply = s.recv(4096)
        print(reply)
        s.close()
        print("File sent for printing.\n")
    except:
        print("Error sending file: \n")
        sys.exit()
    server
    Code:
    import socket
    
    
    
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.bind(('', 8989))
        s.listen(10)
        conn, addr = s.accept()
        file = open('/tmp/fold/test.docx', 'a+b')
        while 1:
            print("starting set. \n")
    #        conn, addr = s.accept()
            print("accepting conn")
            data = conn.recv(4096)
            if not data:
                break
            print("recieve data")
            #Copy file here.... Temp until we get the printer working
            file.write(data)
            print("Writing data... \n")
    except:
        print("Could not load server... \n")
    
    file.close()
    print("File closed")
    conn.close()
    print("conn closed")
    s.close()
    print("sock closed")
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    1
    Rep Power
    0
    I'm new to Python and this forum but I think the answer to your question lies here:

    data = file.read(4096)

    Hope it helps.

IMN logo majestic logo threadwatch logo seochat tools logo