Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'argument': 0.04; 'filename': 0.07; 'ok.': 0.07; 'subject:file': 0.07; 'python': 0.09; "'w')": 0.09; 'fp:': 0.09; 'loop.': 0.09; 'overwrite': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'unexpected': 0.09; 'receives': 0.13; 'bytes).': 0.16; 'codec': 0.16; 'decode': 0.16; 'incomplete': 0.16; 'iteration': 0.16; 'message- id:@dough.gmane.org': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'true:': 0.16; 'wrote:': 0.17; 'byte': 0.17; 'bytes': 0.17; '>>>': 0.18; 'file.': 0.20; 'do.': 0.21; '"",': 0.22; 'exists.': 0.22; 'tells': 0.22; 'header :User-Agent:1': 0.26; 'leave': 0.26; '(most': 0.27; '(as': 0.27; 'header:X-Complaints-To:1': 0.28; 'probably': 0.29; 'problem.': 0.32; 'file': 0.32; 'good.': 0.32; 'traceback': 0.33; 'to:addr :python-list': 0.33; 'code:': 0.33; 'skip:d 20': 0.34; "can't": 0.34; 'skip:b 20': 0.34; 'server': 0.35; 'open': 0.35; 'too.': 0.35; 'received:org': 0.36; 'but': 0.36; 'client': 0.36; 'subject: (': 0.36; 'far': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'subject:-': 0.40; 'header:Received:5': 0.40; 'end': 0.40; 'skip:u 10': 0.60; 'first': 0.61; 'thomas': 0.62; 'ever': 0.63; 'receive': 0.71; 'subject:content': 0.84; 'subject:write': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: newbie: write content in a file (server-side) Date: Sun, 29 Jul 2012 17:16:11 +0200 Organization: None References: <98f9b4a6-b797-40f3-a919-89c2d4fb4496@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Gmane-NNTP-Posting-Host: p5084be5a.dip.t-dialin.net User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 44 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1343574970 news.xs4all.nl 6939 [2001:888:2000:d::a6]:33526 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:26196 Thomas Kaufmann wrote: > I send from a client file content to my server (as bytes). So far so good. > The server receives this content complete. Ok. Then I want to write this > content to a new file. It works too. But in the new file are only the > first part of the whole content. > > What's the problem. > Here's my server code: > 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]) - Do you ever want to leave the loop? - You calculate a new filename on every iteration of the while loop -- probably not what you intended to do. - The "w" argument tells Python to overwrite the file if it exists. You either need to keep the file open (move the with... out of the loop) or open it with "a". - You may not receive the complete file name on the first iteration of the while loop. - The bytes buffer can contain incomplete characters, e. g.: >>> data = b"\xc3\xa4" >>> data.decode("utf-8") 'รค' >>> data[:1].decode("utf-8") Traceback (most recent call last): File "", line 1, in UnicodeDecodeError: 'utf8' codec can't decode byte 0xc3 in position 0: unexpected end of data