Re: Transferring a file over sockets

Discussion in 'Python' started by Tim Golden, Dec 17, 2008.

  1. Tim Golden

    Tim Golden Guest

    Ferdinand Sousa wrote:
    > I am using sockets to transfer a file over LAN. There are 2 scripts, the
    > server opens a listens for connection and the client is run on another
    > machine. I always make sure the server is run first. The strange thing is
    > that if the the server script is double-clicked and executed (run in a
    > console with title %Python path%python.exe) the output file saved on the
    > server is truncated. It works just fine if you open the server script in
    > IDLE and then run it. The client script can be run in either way, it still
    > works. You could try using any arbitrary file to test this behaviour after
    > changing the file name in both the scripts.


    A couple of things:

    * Are you aware that there's no particular guarantee
    that what's sent along a socket in one write will
    be received in one read? There's a HOWTO here if
    you're interested:

    http://docs.python.org/dev/howto/sockets.html

    * Also, unless this is an exercise for yourself in investigating
    how sockets work, (in which case... read the docs :)), you might
    consider that you're reinventing the wheel not a little. But
    perhaps you knew that?

    TJG

    >
    > ==========================================================
    > # file receiver
    > # work in progress
    >
    > import socket
    >
    > s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    > HOST = '192.168.1.17'
    > PORT = 31400
    >
    > s.bind((HOST, PORT))
    > s.listen(3)
    > conn, addr = s.accept()
    > print 'conn at address',addr
    > conn.send('READY')
    > f = open('C:\\Documents and Settings\\USER\\Desktop\\test.pdf','wb')
    > fsize=int(conn.recv(8))
    > print 'File size',fsize
    > f.write(conn.recv(fsize))
    > f.close()
    > conn.close()
    > s.close()
    >
    > raw_input('Press any key to exit')
    >
    >
    > ===========================================================
    >
    > # file sender !!!
    > # Work in progress
    >
    > import socket, os
    > from stat import ST_SIZE
    >
    >
    > HOST = '192.168.1.17'
    > PORT = 31400 # Arbitrary non-privileged port
    >
    > s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    >
    > s.connect((HOST,PORT))
    > if s.recv(5)!='READY':
    > raw_input('Unable to connect \n\n Press any key to exit ...')
    > s.close()
    > exit()
    >
    > f=open('C:\\Documents and Settings\\USER\\Desktop\\t.pdf', 'rb')
    > fsize=os.stat(f.name)[ST_SIZE]
    >
    > s.send(str(fsize))
    > s.send(f.read())
    >
    > s.close()
    > f.close()
    >
    > ===========================================================
    >
    > Thanks for reading!!
    >
    > Best regards,
    > Ferdi
    >
    >
    >
    > ------------------------------------------------------------------------
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
     
    Tim Golden, Dec 17, 2008
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. fox_fire

    Sockets and file transferring

    fox_fire, Apr 6, 2004, in forum: Java
    Replies:
    0
    Views:
    373
    fox_fire
    Apr 6, 2004
  2. Laco
    Replies:
    2
    Views:
    347
  3. polaris venus via JavaKB.com

    transferring file through socket

    polaris venus via JavaKB.com, Apr 11, 2005, in forum: Java
    Replies:
    4
    Views:
    1,445
    Gordon Beaton
    Apr 12, 2005
  4. Murali
    Replies:
    1
    Views:
    367
    Ian Collins
    Apr 18, 2006
  5. Replies:
    2
    Views:
    985
Loading...

Share This Page