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: socketserver example code not correctly ported to py3k
Type: Stage:
Components: Versions: Python 3.0
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, bmiller, macd, vstinner
Priority: release blocker Keywords: patch

Created on 2008-11-07 03:29 by macd, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
socketserverdoc.patch bmiller, 2008-11-07 22:48
unnamed bmiller, 2008-11-08 13:19
socketpatches.patch bmiller, 2008-11-08 13:47
Messages (7)
msg75595 - (view) Author: Don MacMillen (macd) Date: 2008-11-07 03:29
code examples in socketserver do not run in py3k
Obvious errors with print stmt (not function call)
Less obvious errors with socket.send that does not
accept str type (bytearray works fine). Client example
below shows problems.

import socket
import sys

HOST, PORT = "localhost", 9999
data = " ".join(sys.argv[1:])

# Create a socket (SOCK_STREAM means a TCP socket)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Connect to server and send data
sock.connect((HOST, PORT))
sock.send(data + "\n")

# Receive data from the server and shut down
received = sock.recv(1024)
sock.close()

print "Sent:     %s" % data
print "Received: %s" % received
msg75612 - (view) Author: Brad Miller (bmiller) Date: 2008-11-07 20:03
I found a similar problem in the Demo/sockets/unixclient.py code.

from socket import *

FILE = 'unix-socket'
s = socket(AF_UNIX, SOCK_STREAM)
s.connect(FILE)
s.send('Hello, world')
data = s.recv(1024)
s.close()
print('Received', repr(data))

Produces the following error message:

Traceback (most recent call last):
  File "unixclient.py", line 9, in <module>
    s.send('Hello, world')
TypeError: send() argument 1 must be string or buffer, not str

My question is around whether the examples are wrong and 'Hello, World'
should simply be wrapped with bytearray('Hello, World','utf8')   or
whether the underlying socket implementation is wrong.  Judging by the
error message above it looks like the implementation is catching just
this kind of error and the example should be changed.  But, this must
break backward compatibility all over the place.  And since the bug has
release blocker it makes me think the socket implementation should be
changed.
msg75620 - (view) Author: Brad Miller (bmiller) Date: 2008-11-07 22:48
After looking at the socket documentation pointed to from another issue
it looks like the right solution is to convert to a byte array.

I've attached a patch with fixes for all the examples in
socketserver.rst  there were several other problems that I think were
unrelated to Python 3.0 that I cleaned up in the process.
msg75630 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2008-11-08 11:48
Why not using bytes() instead of bytearray()? Eg. replace 
s.send('Hello, world') by s.send(b'Hello, world').
msg75636 - (view) Author: Brad Miller (bmiller) Date: 2008-11-08 13:19
For the example in unixclient.py using b'Hello World' works fine.  But for
the example in the socketserver documentation the strings to convert come
from argv[1:]

On Sat, Nov 8, 2008 at 5:48 AM, STINNER Victor <report@bugs.python.org>wrote:

>
> STINNER Victor <victor.stinner@haypocalc.com> added the comment:
>
> Why not using bytes() instead of bytearray()? Eg. replace
> s.send('Hello, world') by s.send(b'Hello, world').
>
> ----------
> nosy: +haypo
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue4275>
> _______________________________________
>
msg75637 - (view) Author: Brad Miller (bmiller) Date: 2008-11-08 13:47
Here's a combined patch that fixes:

Doc/library/socketserver.rst  examples tested and working
Demo/sockets/udpecho.py
Demo/sockets/unixclient.py
msg75640 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2008-11-08 17:24
Thanks! Fixed in r67168.
History
Date User Action Args
2022-04-11 14:56:41adminsetgithub: 48525
2008-11-08 17:24:43benjamin.petersonsetstatus: open -> closed
resolution: fixed
messages: + msg75640
nosy: + benjamin.peterson
2008-11-08 13:47:28bmillersetfiles: + socketpatches.patch
messages: + msg75637
2008-11-08 13:19:06bmillersetfiles: + unnamed
messages: + msg75636
2008-11-08 11:48:05vstinnersetnosy: + vstinner
messages: + msg75630
2008-11-07 22:49:02bmillersetfiles: + socketserverdoc.patch
keywords: + patch
messages: + msg75620
2008-11-07 20:03:29bmillersetnosy: + bmiller
messages: + msg75612
2008-11-07 03:59:20benjamin.petersonsetpriority: release blocker
2008-11-07 03:29:36macdcreate