Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.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; 'to:addr:comp.lang.python': 0.09; 'unexpected': 0.09; 'cc:addr :python-list': 0.10; 'receives': 0.13; 'bytes).': 0.16; 'codec': 0.16; 'decode': 0.16; 'incomplete': 0.16; 'iteration': 0.16; 'sonntag,': 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; 'cc:2**0': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply- To:1': 0.25; 'header:User-Agent:1': 0.26; 'leave': 0.26; '(most': 0.27; '(as': 0.27; 'probably': 0.29; 'problem.': 0.32; 'file': 0.32; 'good.': 0.32; 'traceback': 0.33; 'code:': 0.33; 'skip:d 20': 0.34; "can't": 0.34; 'skip:b 20': 0.34; 'received:google.com': 0.34; 'thanks': 0.34; 'server': 0.35; 'from:addr:googlemail.com': 0.35; 'open': 0.35; 'too.': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'client': 0.36; 'subject: (': 0.36; 'received:209': 0.37; 'far': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'subject:-': 0.40; 'end': 0.40; 'skip:u 10': 0.60; 'first': 0.61; 'thomas': 0.62; 'ever': 0.63; 'receive': 0.71; '29.': 0.84; 'subject:content': 0.84; 'subject:write': 0.84 Newsgroups: comp.lang.python Date: Mon, 30 Jul 2012 02:50:12 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=88.73.121.54; posting-account=fVeGmgoAAACSXJJZA2P7ITa2D-cVKHno References: <98f9b4a6-b797-40f3-a919-89c2d4fb4496@googlegroups.com> User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 88.73.121.54 MIME-Version: 1.0 Subject: Re: newbie: write content in a file (server-side) From: Thomas Kaufmann To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org 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: , Message-ID: Lines: 92 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1343641814 news.xs4all.nl 6968 [2001:888:2000:d::a6]:33504 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:26238 Am Sonntag, 29. Juli 2012 17:16:11 UTC+2 schrieb Peter Otten: > Thomas Kaufmann wrote: >=20 >=20 >=20 > > I send from a client file content to my server (as bytes). So far so go= od. >=20 > > The server receives this content complete. Ok. Then I want to write thi= s >=20 > > content to a new file. It works too. But in the new file are only the >=20 > > first part of the whole content. >=20 > >=20 >=20 > > What's the problem. >=20 >=20 >=20 > > Here's my server code: >=20 >=20 >=20 > > while True: >=20 > > bytes =3D self.request.recv(4096) >=20 > > if bytes: >=20 > > s =3D bytes.decode("utf8") >=20 > > print(s) >=20 > > li =3D s.split("~") >=20 > > with open(li[0], 'w') as fp: >=20 > > fp.write(li[1]) >=20 >=20 >=20 > - Do you ever want to leave the loop? >=20 >=20 >=20 > - You calculate a new filename on every iteration of the while loop --=20 >=20 > probably not what you intended to do. >=20 >=20 >=20 > - The "w" argument tells Python to overwrite the file if it exists. You= =20 >=20 > either need to keep the file open (move the with... out of the loop) or o= pen=20 >=20 > it with "a". >=20 >=20 >=20 > - You may not receive the complete file name on the first iteration of th= e=20 >=20 > while loop. >=20 >=20 >=20 > - The bytes buffer can contain incomplete characters, e. g.: >=20 >=20 >=20 > >>> data =3D b"\xc3\xa4" >=20 > >>> data.decode("utf-8") >=20 > '=E4' >=20 > >>> data[:1].decode("utf-8") >=20 > Traceback (most recent call last): >=20 > File "", line 1, in >=20 > UnicodeDecodeError: 'utf8' codec can't decode byte 0xc3 in position 0:=20 >=20 > unexpected end of data Thanks Peter. It helps;-).