newbie: write new file (from a server)

Discussion in 'Python' started by tokauf@googlemail.com, Jul 29, 2012.

  1. Guest

    Hi,

    I have a client. He sends file content (as bytes) to my server. The server receives this content as bytes and decodes it to string. Then the server opens a file (filename comes from client) try to write the file-content to the new file.
    It works but there are parts of the client file content in the new file.

    I tested it: the whole content from client comes to the server.

    Can anybody help me?

    My server code:

    -------------------------

    import socketserver

    class MyTCPServer(socketserver.BaseRequestHandler):

    def handle(self):

    s = ''
    li = []
    addr = self.client_address[0]
    print("[{}] Connected! ".format(addr))
    while True:

    bytes = self.request.recv(4096)
    if bytes:
    s = bytes.decode("utf8")
    print(s)
    li = s.split("~")
    with open(li[0], 'w') as fp:
    fp.write(li[1])

    #... main ......................................................

    if __name__ == "__main__":

    server = socketserver.ThreadingTCPServer(("", 12345), MyTCPServer)
    server.serve_forever()

    --------------------------------


    o-o

    Thomas
     
    , Jul 29, 2012
    #1
    1. Advertisements

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:
    663
    Alan Krueger
    Jul 29, 2005
  2. Peter Hansen
    Replies:
    1
    Views:
    499
    Joona I Palaste
    Aug 24, 2003
  3. Peter Hansen
    Replies:
    1
    Views:
    519
    Joona I Palaste
    Aug 24, 2003
  4. scad
    Replies:
    4
    Views:
    1,188
    James Kanze
    May 28, 2009
  5. Erik Veenstra

    File.new("file.ext", "w").write data

    Erik Veenstra, Jul 22, 2004, in forum: Ruby
    Replies:
    5
    Views:
    266
    Robert Klemme
    Jul 23, 2004
  6. Iulian Ilea
    Replies:
    1
    Views:
    484
    pcx99
    Dec 21, 2006
  7. Replies:
    2
    Views:
    789
    Thomas 'PointedEars' Lahn
    Mar 11, 2008
  8. Thomas Kaufmann

    newbie: write content in a file (server-side)

    Thomas Kaufmann, Jul 29, 2012, in forum: Python
    Replies:
    4
    Views:
    274
    Thomas Kaufmann
    Jul 30, 2012
Loading...