socket.makefile() questions
My apologies if this message is to the wrong group. I have been experimenting with socket.makefile()from Python 3.1.1. I have not had much difficulty reading from the returned file object, but I don't understand the behavior when trying to write (send on the socket). I'm hoping that someone can explain how this is supposed to work. I find that this works for an established connection on socket s: fd = s.makefile('wb', buffering = 0) fd.write("This is a test message\n".encode('ascii')) A mode of 'rwb' also works. The object fd is of type SocketIO. fd = s.makefile('w', buffering = 0) -> ValueError exception fd = s.makefile('w') -> io.BufferedWriter, which does not send data. fd = s.makefile('wb') -> io.TextIOWrapper, which does not send data. The default value of the "buffering" parameter is None, which from my testing has a different result than 0 (zero). So, questions: 1) Why does buffering = None result in a buffered file object? 2) Are there bugs or incomplete work with socket.makefile(), io.BufferedWriter and io.TextIOWrapper in terms of why the latter two objects are returned, but fail to send data? Thank you, -- Tim Bower Assistant Professor Kansas State University at Salina Computer Systems Technology tim@ksu.edu
2010/1/31 <tim@ksu.edu>:
My apologies if this message is to the wrong group.
I have been experimenting with socket.makefile()from Python 3.1.1. I have not had much difficulty reading from the returned file object, but I don't understand the behavior when trying to write (send on the socket). I'm hoping that someone can explain how this is supposed to work.
I find that this works for an established connection on socket s: fd = s.makefile('wb', buffering = 0) fd.write("This is a test message\n".encode('ascii'))
A mode of 'rwb' also works. The object fd is of type SocketIO.
fd = s.makefile('w', buffering = 0) -> ValueError exception fd = s.makefile('w') -> io.BufferedWriter, which does not send data. fd = s.makefile('wb') -> io.TextIOWrapper, which does not send data.
The default value of the "buffering" parameter is None, which from my testing has a different result than 0 (zero).
So, questions: 1) Why does buffering = None result in a buffered file object? 2) Are there bugs or incomplete work with socket.makefile(), io.BufferedWriter and io.TextIOWrapper in terms of why the latter two objects are returned, but fail to send data?
It sounds like that function is broken and buggy in python 3 and a few bug reports need to be filed at bugs.python.org. -- Regards, Benjamin
You are correct that fd.flush() makes a difference. I thought that flush() would only affect the ability to read after the write, but actually the data was not sent until the flush() operation. I'll do some more testing. Thank you. -- Tim Bower Assistant Professor Kansas State University at Salina Computer Systems Technology tim@ksu.edu 785-826-2920 ----- Original Message ----- From: "Antoine Pitrou" <solipsis@pitrou.net> To: "stdlib-sig" <stdlib-sig@python.org> Sent: Sunday, January 31, 2010 5:18:34 PM GMT -06:00 US/Canada Central Subject: Re: [stdlib-sig] socket.makefile() questions
fd = s.makefile('w', buffering = 0) -> ValueError exception fd = s.makefile('w') -> io.BufferedWriter, which does not send data. fd = s.makefile('wb') -> io.TextIOWrapper, which does not send data.
Have you tried fd.flush() after writing your data? _______________________________________________ stdlib-sig mailing list stdlib-sig@python.org http://mail.python.org/mailman/listinfo/stdlib-sig
fd.flush() seems to be the secret - worked as expected in all tests. If possible, I'd like to suggest that the documentation be updated to explicitly say that a buffered file object is returned unless the buffering parameter is set to 0 (zero). Setting buffering = None (default value) still returns a buffered object, which seems a little counter intuitive, but acceptable if the documentation makes it clear. Thank you. -- Tim Bower Assistant Professor Kansas State University at Salina Computer Systems Technology tim@ksu.edu ----- Original Message ----- From: "Timothy Bower" <tim@ksu.edu> To: "Antoine Pitrou" <solipsis@pitrou.net> Cc: "stdlib-sig" <stdlib-sig@python.org> Sent: Sunday, January 31, 2010 5:37:20 PM GMT -06:00 US/Canada Central Subject: Re: [stdlib-sig] socket.makefile() questions You are correct that fd.flush() makes a difference. I thought that flush() would only affect the ability to read after the write, but actually the data was not sent until the flush() operation. I'll do some more testing. Thank you. -- Tim Bower Assistant Professor Kansas State University at Salina Computer Systems Technology tim@ksu.edu ----- Original Message ----- From: "Antoine Pitrou" <solipsis@pitrou.net> To: "stdlib-sig" <stdlib-sig@python.org> Sent: Sunday, January 31, 2010 5:18:34 PM GMT -06:00 US/Canada Central Subject: Re: [stdlib-sig] socket.makefile() questions
fd = s.makefile('w', buffering = 0) -> ValueError exception fd = s.makefile('w') -> io.BufferedWriter, which does not send data. fd = s.makefile('wb') -> io.TextIOWrapper, which does not send data.
Have you tried fd.flush() after writing your data? _______________________________________________ stdlib-sig mailing list stdlib-sig@python.org http://mail.python.org/mailman/listinfo/stdlib-sig _______________________________________________ stdlib-sig mailing list stdlib-sig@python.org http://mail.python.org/mailman/listinfo/stdlib-sig
On Sun, 31 Jan 2010 18:56:54 -0800, Timothy Bower <tim@ksu.edu> wrote:
fd.flush() seems to be the secret - worked as expected in all tests. If possible, I'd like to suggest that the documentation be updated to explicitly say that a buffered file object is returned unless the buffering parameter is set to 0 (zero). Setting buffering = None (def ault value) still returns a buffered object, which seems a little counter intuitive, but acceptable if the documentation makes it clear.
It is already documented. socket.makefile says the args are interpreted as for the built in function 'open' and provides a link to those docs, which clearly say that the default is that buffering is on. None is often used to mean "default" in Python functions. As for your original question as to whether or not this is the right list, no it isn't :). python-list is the best place to post this kind of question, and then if you find you have a real bug, file a bug report on the tracker. -- R. David Murray www.bitdance.com Business Process Automation - Network/Server Management - Routers/Firewalls
participants (5)
-
Antoine Pitrou
-
Benjamin Peterson
-
R. David Murray
-
tim@ksu.edu
-
Timothy Bower