Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
06fd5f8c
Commit
06fd5f8c
authored
Nov 08, 2008
by
Benjamin Peterson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix the socketserver demo code for py3k
#4275 Thanks to Don MacMillen
parent
ef6a19e3
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
38 additions
and
30 deletions
+38
-30
Demo/sockets/udpecho.py
Demo/sockets/udpecho.py
+2
-1
Demo/sockets/unixclient.py
Demo/sockets/unixclient.py
+1
-1
Doc/library/socketserver.rst
Doc/library/socketserver.rst
+29
-28
Misc/ACKS
Misc/ACKS
+1
-0
Misc/NEWS
Misc/NEWS
+5
-0
No files found.
Demo/sockets/udpecho.py
View file @
06fd5f8c
...
...
@@ -56,7 +56,8 @@ def client():
line
=
sys
.
stdin
.
readline
()
if
not
line
:
break
s
.
sendto
(
line
,
addr
)
print
(
'addr = '
,
addr
)
s
.
sendto
(
bytes
(
line
,
'ascii'
),
addr
)
data
,
fromaddr
=
s
.
recvfrom
(
BUFSIZE
)
print
(
'client received %r from %r'
%
(
data
,
fromaddr
))
...
...
Demo/sockets/unixclient.py
View file @
06fd5f8c
...
...
@@ -6,7 +6,7 @@ from socket import *
FILE
=
'unix-socket'
s
=
socket
(
AF_UNIX
,
SOCK_STREAM
)
s
.
connect
(
FILE
)
s
.
send
(
'Hello, world'
)
s
.
send
(
b
'Hello, world'
)
data
=
s
.
recv
(
1024
)
s
.
close
()
print
(
'Received'
,
repr
(
data
))
Doc/library/socketserver.rst
View file @
06fd5f8c
...
...
@@ -336,8 +336,8 @@ This is the server side::
def handle(self):
# self.request is the TCP socket connected to the client
self.data = self.request.recv(1024).strip()
print
"%s wrote:" % self.client_address[0]
print
self.data
print
("%s wrote:" % self.client_address[0])
print
(self.data)
# just send back the same data, but upper-cased
self.request.send(self.data.upper())
...
...
@@ -360,8 +360,8 @@ objects that simplify communication by providing the standard file interface)::
# self.rfile is a file-like object created by the handler;
# we can now use e.g. readline() instead of raw recv() calls
self.data = self.rfile.readline().strip()
print
"%s wrote:" % self.client_address[0]
print
self.data
print
("%s wrote:" % self.client_address[0])
print
(self.data)
# Likewise, self.wfile is a file-like object used to write back
# to the client
self.wfile.write(self.data.upper())
...
...
@@ -385,14 +385,14 @@ This is the client side::
# Connect to server and send data
sock.connect((HOST, PORT))
sock.send(
data + "\n"
)
sock.send(
bytes(data + "\n","utf8")
)
# Receive data from the server and shut down
received = sock.recv(1024)
sock.close()
print
"Sent: %s" % data
print
"Received: %s" % received
print
("Sent: %s" % data)
print
("Received: %s" % received)
The output of the example should look something like this:
...
...
@@ -401,18 +401,18 @@ Server::
$ python TCPServer.py
127.0.0.1 wrote:
hello world with TCP
b'hello world with TCP'
127.0.0.1 wrote:
python is nice
b'python is nice'
Client::
$ python TCPClient.py hello world with TCP
Sent: hello world with TCP
Received:
HELLO WORLD WITH TCP
Received:
b'HELLO WORLD WITH TCP'
$ python TCPClient.py python is nice
Sent: python is nice
Received:
PYTHON IS NICE
Received:
b'PYTHON IS NICE'
:class:`socketserver.UDPServer` Example
...
...
@@ -433,13 +433,13 @@ This is the server side::
def handle(self):
data = self.request[0].strip()
socket = self.request[1]
print
"%s wrote:" % self.client_address[0]
print
data
print
("%s wrote:" % self.client_address[0])
print
(data)
socket.sendto(data.upper(), self.client_address)
if __name__ == "__main__":
HOST, PORT = "localhost", 9999
server = socketserver.UDPServer((HOST, PORT),
BaseUDPRequest
Handler)
server = socketserver.UDPServer((HOST, PORT),
MyUDP
Handler)
server.serve_forever()
This is the client side::
...
...
@@ -447,7 +447,7 @@ This is the client side::
import socket
import sys
HOST, PORT = "localhost"
HOST, PORT = "localhost"
, 9999
data = " ".join(sys.argv[1:])
# SOCK_DGRAM is the socket type to use for UDP sockets
...
...
@@ -455,11 +455,11 @@ This is the client side::
# As you can see, there is no connect() call; UDP has no connections.
# Instead, data is directly sent to the recipient via sendto().
sock.sendto(
data + "\n"
, (HOST, PORT))
sock.sendto(
bytes(data + "\n","utf8")
, (HOST, PORT))
received = sock.recv(1024)
print
"Sent: %s" % data
print
"Received: %s" % received
print
("Sent: %s" % data)
print
("Received: %s" % received)
The output of the example should look exactly like for the TCP server example.
...
...
@@ -481,7 +481,7 @@ An example for the :class:`ThreadingMixIn` class::
def handle(self):
data = self.request.recv(1024)
cur_thread = threading.current_thread()
response =
"%s: %s" % (cur_thread.get_name(), data
)
response =
bytes("%s: %s" % (cur_thread.getName(), data),'ascii'
)
self.request.send(response)
class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
...
...
@@ -492,7 +492,7 @@ An example for the :class:`ThreadingMixIn` class::
sock.connect((ip, port))
sock.send(message)
response = sock.recv(1024)
print
"Received: %s" % response
print
("Received: %s" % response)
sock.close()
if __name__ == "__main__":
...
...
@@ -506,23 +506,24 @@ An example for the :class:`ThreadingMixIn` class::
# more thread for each request
server_thread = threading.Thread(target=server.serve_forever)
# Exit the server thread when the main thread terminates
server_thread.set
_d
aemon(True)
server_thread.set
D
aemon(True)
server_thread.start()
print
"Server loop running in thread:", t.get_name(
)
print
("Server loop running in thread:", server_thread.getName()
)
client(ip, port, "Hello World 1")
client(ip, port, "Hello World 2")
client(ip, port, "Hello World 3")
client(ip, port,
b
"Hello World 1")
client(ip, port,
b
"Hello World 2")
client(ip, port,
b
"Hello World 3")
server.shutdown()
The output of the example should look something like this::
$ python ThreadedTCPServer.py
Server loop running in thread: Thread-1
Received:
Thread-2: Hello World 1
Received:
Thread-3: Hello World 2
Received:
Thread-4: Hello World 3
Received:
b"Thread-2: b'Hello World 1'"
Received:
b"Thread-3: b'Hello World 2'"
Received:
b"Thread-4: b'Hello World 3'"
The :class:`ForkingMixIn` class is used in the same way, except that the server
...
...
Misc/ACKS
View file @
06fd5f8c
...
...
@@ -435,6 +435,7 @@ Martin von L
Andrew I MacIntyre
Tim MacKenzie
Nick Maclaren
Don MacMillen
Steve Majewski
Grzegorz Makarewicz
Ken Manheimer
...
...
Misc/NEWS
View file @
06fd5f8c
...
...
@@ -23,6 +23,11 @@ Build
- Issue #1656675: Register a drop handler for .py* files on Windows.
Tools/Demos
-----------
- Demos of the socketserver module now work with Python 3.
What's New in Python 3.0 release candidate 2
============================================
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment