Issue 36047: socket file handle does not support stream write

Issue36047

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