Client server implementation

Discussion in 'Python' started by Babloo, Sep 15, 2008.

  1. Babloo

    Babloo Guest

    Hi everyone,

    i wanted to implement a client- server connection
    and transfer a file over the network. i was able to implement a normal
    set up where in i was able to send chat messages and small chunks of
    data.
    I want to implement the file transfer with out using the FTP library
    available in python .

    So can anyone suggest a piece of code by which i can
    do that.

    Thanks
    Pruthvi
    Babloo, Sep 15, 2008
    #1
    1. Advertising

  2. Babloo

    Larry Bates Guest

    Babloo wrote:
    > Hi everyone,
    >
    > i wanted to implement a client- server connection
    > and transfer a file over the network. i was able to implement a normal
    > set up where in i was able to send chat messages and small chunks of
    > data.
    > I want to implement the file transfer with out using the FTP library
    > available in python .
    >
    > So can anyone suggest a piece of code by which i can
    > do that.
    >
    > Thanks
    > Pruthvi


    What Client OS? What Server OS? On LAN or Internet? You need to give is some
    information to help you.

    -Larry
    Larry Bates, Sep 15, 2008
    #2
    1. Advertising

  3. Babloo

    Benjamin Guest

    On Sep 15, 5:15 am, Babloo <> wrote:
    > Hi everyone,
    >
    >                   i wanted to implement a client- server connection
    > and transfer a file over the network. i was able to implement a normal
    > set up where in i was able to send chat messages and small chunks of
    > data.
    > I want to implement the file transfer with out using the FTP library
    > available in python .
    >
    >                   So can anyone suggest a piece of code by which i can
    > do that.


    I don't know what you really want, but look at Twisted. http://twistedmatrix.com

    >
    > Thanks
    > Pruthvi
    Benjamin, Sep 16, 2008
    #3
  4. Babloo

    Babloo Guest

    On Sep 16, 7:38 am, Benjamin <> wrote:
    > On Sep 15, 5:15 am, Babloo <> wrote:
    >
    > > Hi everyone,

    >
    > >                   i wanted to implement a client- server connection
    > > and transfer a file over the network. i was able to implement a normal
    > > set up where in i was able to send chat messages and small chunks of
    > > data.
    > > I want to implement the file transfer with out using the FTP library
    > > available in python .

    >
    > >                   So can anyone suggest a piece of code by which i can
    > > do that.

    >
    > I don't know what you really want, but look at Twisted.http://twistedmatrix.com
    >
    >
    >
    > > Thanks
    > > Pruthvi

    >
    >

    I am beginner at using python language.
    I mean i am implementing that in a windows based machine and wanted
    to transfer files over the LAN. I have written down two files as
    client.py and server.py . I ran these files at two different Computers
    in the LAN and I was able to send small chunks of messages( like
    chat).

    I was successful in establishing connection and sending small
    messages. But now i want to transfer a complete file with out using
    the ftplib(FTP LIBRARY) in python.


    The files look like this.i hope this would help.

    ______________________________________________
    # Server program

    from socket import *

    # Set the socket parameters
    host = "117.105.224.94"
    port = 21567
    buf = 1024
    addr = (host,port)

    # Create socket and bind to address
    UDPSock = socket(AF_INET,SOCK_DGRAM)
    UDPSock.bind(addr)

    # Receive messages
    while 1:
    data,addr = UDPSock.recvfrom(buf)
    if not data:
    print "Client has exited!"
    break
    else:
    print "\nReceived message '", data,"'"

    # Close socket
    UDPSock.close()
    __________________________________________________

    # Client program

    from socket import *

    # Set the socket parameters
    host = "117.105.224.94"
    port = 21567
    buf = 1024
    addr = (host,port)

    # Create socket
    UDPSock = socket(AF_INET,SOCK_DGRAM)

    def_msg = "===Enter message to send to server===";
    print "\n",def_msg

    # Send messages
    while (1):
    data = raw_input('>> ')
    if not data:
    break
    else:
    if(UDPSock.sendto(data,addr)):
    print "Sending message '",data,"'....."

    # Close socket
    UDPSock.close()

    _____________________________________________________________
    Babloo, Sep 16, 2008
    #4
  5. Babloo

    r0g Guest

    Babloo wrote:
    > On Sep 16, 7:38 am, Benjamin <> wrote:
    >> On Sep 15, 5:15 am, Babloo <> wrote:
    >>
    >>> Hi everyone,
    >>> i wanted to implement a client- server connection
    >>> and transfer a file over the network. i was able to implement a normal
    >>> set up where in i was able to send chat messages and small chunks of
    >>> data.
    >>> I want to implement the file transfer with out using the FTP library
    >>> available in python .
    >>> So can anyone suggest a piece of code by which i can
    >>> do that.

    >> I don't know what you really want, but look at Twisted.http://twistedmatrix.com
    >>
    >>
    >>
    >>> Thanks
    >>> Pruthvi

    >>

    > I am beginner at using python language.
    > I mean i am implementing that in a windows based machine and wanted
    > to transfer files over the LAN. I have written down two files as
    > client.py and server.py . I ran these files at two different Computers
    > in the LAN and I was able to send small chunks of messages( like
    > chat).
    >
    > I was successful in establishing connection and sending small
    > messages. But now i want to transfer a complete file with out using
    > the ftplib(FTP LIBRARY) in python.
    >
    >
    > The files look like this.i hope this would help.
    >
    > ______________________________________________
    > # Server program
    >
    > from socket import *
    >
    > # Set the socket parameters
    > host = "117.105.224.94"
    > port = 21567
    > buf = 1024
    > addr = (host,port)
    >
    > # Create socket and bind to address
    > UDPSock = socket(AF_INET,SOCK_DGRAM)
    > UDPSock.bind(addr)
    >
    > # Receive messages
    > while 1:
    > data,addr = UDPSock.recvfrom(buf)
    > if not data:
    > print "Client has exited!"
    > break
    > else:
    > print "\nReceived message '", data,"'"
    >
    > # Close socket
    > UDPSock.close()
    > __________________________________________________
    >
    > # Client program
    >
    > from socket import *
    >
    > # Set the socket parameters
    > host = "117.105.224.94"
    > port = 21567
    > buf = 1024
    > addr = (host,port)
    >
    > # Create socket
    > UDPSock = socket(AF_INET,SOCK_DGRAM)
    >
    > def_msg = "===Enter message to send to server===";
    > print "\n",def_msg
    >
    > # Send messages
    > while (1):
    > data = raw_input('>> ')
    > if not data:
    > break
    > else:
    > if(UDPSock.sendto(data,addr)):
    > print "Sending message '",data,"'....."
    >
    > # Close socket
    > UDPSock.close()
    >
    > _____________________________________________________________
    >



    You're really not far away then, you basically need to come up with a
    simple protocol that can transmit

    1) The file name
    2) The file length
    3) The file data itself

    You could also transmit the files metadata: datestamps, permissions etc
    if you wanted to make it better. Also, there might be security issues to
    consider too if you're running this on anything other than your own home
    network.

    So anyway, for a simple solution howabout you send:

    filename + "\n"
    filesize + "\n" (make sure you use str() to send this as text)

    Then loop through your filedata
    send a chunk
    wait for an OK back from the other side
    loop

    At the receiving end as the chunks arrive:

    Get the filename
    Open a file for writing
    Get the filesize
    Setup a loop to receive the file data
    Send OK
    Get chunk from socket
    Write data to file
    Break (stop) if filesize reached
    Loop
    Close file

    This, of course has no error correction and will probably die if you
    have any packet loss on your network, a more robust solution would be to
    use TCP sockets rather than UDP sockets.

    Hope this helps :)


    Roger Heathcote

    http://www.technicalbloke.com
    http://movingtoubuntu.technicalbloke.co.uk
    r0g, Sep 17, 2008
    #5
    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. -
    Replies:
    2
    Views:
    393
    Alan Krueger
    Jul 29, 2005
  2. Michael Tsang
    Replies:
    32
    Views:
    1,105
    Richard Bos
    Mar 1, 2010
  3. Michael Tsang
    Replies:
    54
    Views:
    1,191
    Phil Carmody
    Mar 30, 2010
  4. sanket
    Replies:
    7
    Views:
    997
    Tsung
    Nov 3, 2011
  5. Vipin Vm
    Replies:
    0
    Views:
    79
    Vipin Vm
    Jul 21, 2008
Loading...

Share This Page