Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #26188 > unrolled thread
| Started by | Thomas Kaufmann <tokauf@googlemail.com> |
|---|---|
| First post | 2012-07-29 07:16 -0700 |
| Last post | 2012-07-30 02:51 -0700 |
| Articles | 5 — 2 participants |
Back to article view | Back to comp.lang.python
newbie: write content in a file (server-side) Thomas Kaufmann <tokauf@googlemail.com> - 2012-07-29 07:16 -0700
Re: newbie: write content in a file (server-side) Peter Otten <__peter__@web.de> - 2012-07-29 17:16 +0200
Re: newbie: write content in a file (server-side) Thomas Kaufmann <tokauf@googlemail.com> - 2012-07-30 02:50 -0700
Re: newbie: write content in a file (server-side) Thomas Kaufmann <tokauf@googlemail.com> - 2012-07-30 02:50 -0700
Re: newbie: write content in a file (server-side) Thomas Kaufmann <tokauf@googlemail.com> - 2012-07-30 02:51 -0700
| From | Thomas Kaufmann <tokauf@googlemail.com> |
|---|---|
| Date | 2012-07-29 07:16 -0700 |
| Subject | newbie: write content in a file (server-side) |
| Message-ID | <98f9b4a6-b797-40f3-a919-89c2d4fb4496@googlegroups.com> |
Hi,
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.
o-o
Thomas
Here's 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()
[toc] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2012-07-29 17:16 +0200 |
| Message-ID | <mailman.2692.1343574970.4697.python-list@python.org> |
| In reply to | #26188 |
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 "<stdin>", line 1, in <module>
UnicodeDecodeError: 'utf8' codec can't decode byte 0xc3 in position 0:
unexpected end of data
[toc] | [prev] | [next] | [standalone]
| From | Thomas Kaufmann <tokauf@googlemail.com> |
|---|---|
| Date | 2012-07-30 02:50 -0700 |
| Message-ID | <mailman.2721.1343641814.4697.python-list@python.org> |
| In reply to | #26196 |
Am Sonntag, 29. Juli 2012 17:16:11 UTC+2 schrieb Peter Otten:
> 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 "<stdin>", line 1, in <module>
>
> UnicodeDecodeError: 'utf8' codec can't decode byte 0xc3 in position 0:
>
> unexpected end of data
Thanks Peter. It helps;-).
[toc] | [prev] | [next] | [standalone]
| From | Thomas Kaufmann <tokauf@googlemail.com> |
|---|---|
| Date | 2012-07-30 02:50 -0700 |
| Message-ID | <99c41e29-5e7d-44c8-a0f5-eeb74af6bb49@googlegroups.com> |
| In reply to | #26196 |
Am Sonntag, 29. Juli 2012 17:16:11 UTC+2 schrieb Peter Otten:
> 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 "<stdin>", line 1, in <module>
>
> UnicodeDecodeError: 'utf8' codec can't decode byte 0xc3 in position 0:
>
> unexpected end of data
Thanks Peter. It helps;-).
[toc] | [prev] | [next] | [standalone]
| From | Thomas Kaufmann <tokauf@googlemail.com> |
|---|---|
| Date | 2012-07-30 02:51 -0700 |
| Message-ID | <8767e4fe-6734-4715-8765-f1b58eabd622@googlegroups.com> |
| In reply to | #26188 |
Am Sonntag, 29. Juli 2012 16:16:01 UTC+2 schrieb Thomas Kaufmann:
> Hi,
>
>
>
> 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.
>
>
>
> o-o
>
>
>
> Thomas
>
>
>
> Here's 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()
Thanks a lot. It helps.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web