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. 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. Peter Hansen
    Replies:
    1
    Views:
    369
    Joona I Palaste
    Aug 24, 2003
  2. Peter Hansen
    Replies:
    1
    Views:
    410
    Joona I Palaste
    Aug 24, 2003
  3. scad
    Replies:
    4
    Views:
    939
    James Kanze
    May 28, 2009
  4. Replies:
    2
    Views:
    437
    Thomas 'PointedEars' Lahn
    Mar 11, 2008
  5. Thomas Kaufmann

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

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

Share This Page