This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: socket file handle does not support stream write
Type: behavior Stage: resolved
Components: 2to3 (2.x to 3.x conversion tool) Versions: Python 3.7
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: Windson Yang, matrixise, xuancong84
Priority: normal Keywords:

Created on 2019-02-20 02:32 by xuancong84, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg336035 - (view) Author: wang xuancong (xuancong84) Date: 2019-02-20 02:32
Python3 programmers have forgotten to convert/implement the socket file descriptor for IO stream operation. Would you please add it? Thanks!

import socket
s = socket.socket()
s.connect('localhost', 5432)
S = s.makefile()

# on Python2, the following works
print >>S, 'hello world'
S.flush()

# on Python3, the same thing does not work
print('hello world', file=S, flush=True)

It gives the following error:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
io.UnsupportedOperation: not writable

Luckily, the stream read operation works, S.readline()
msg336048 - (view) Author: Windson Yang (Windson Yang) * Date: 2019-02-20 07:47
From the docs https://docs.python.org/3/library/socket.html#socket.socket.makefile, the default mode for makefile() is 'r' (only readable). In your example, just use S = s.makefile(mode='w') instead.
msg336051 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2019-02-20 09:26
I confirm that you don't use socket.makefile in write mode.

Python 3.7.2 (default, Jan 16 2019, 19:49:22) 
[GCC 8.2.1 20181215 (Red Hat 8.2.1-6)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> s = socket.socket()
>>> s.connect('localhost', 5432)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: connect() takes exactly one argument (2 given)
>>> s.connect(('localhost', 5432))
>>> S = s.makefile()
>>> print('hello world', file=S, flush=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
io.UnsupportedOperation: not writable
>>> S = s.makefile(mode='w')
>>> print('hello world', file=S, flush=True)
>>> 

I close the issue.
History
Date User Action Args
2022-04-11 14:59:11adminsetgithub: 80228
2019-02-20 09:26:37matrixisesetstatus: open -> closed

nosy: + matrixise
messages: + msg336051

resolution: rejected
stage: resolved
2019-02-20 07:47:15Windson Yangsetnosy: + Windson Yang
messages: + msg336048
2019-02-20 02:32:25xuancong84create