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
023611f3
Commit
023611f3
authored
Oct 23, 2011
by
Florent Xicluna
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue 13141: Demonstrate recommended style for socketserver examples.
parent
ce770375
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
40 additions
and
30 deletions
+40
-30
Doc/library/socketserver.rst
Doc/library/socketserver.rst
+34
-30
Misc/NEWS
Misc/NEWS
+6
-0
No files found.
Doc/library/socketserver.rst
View file @
023611f3
...
@@ -348,7 +348,7 @@ This is the server side::
...
@@ -348,7 +348,7 @@ This is the server side::
def handle(self):
def handle(self):
# self.request is the TCP socket connected to the client
# self.request is the TCP socket connected to the client
self.data = self.request.recv(1024).strip()
self.data = self.request.recv(1024).strip()
print("
%s wrote:" % self.client_address[0]
)
print("
{} wrote:".format(self.client_address[0])
)
print(self.data)
print(self.data)
# just send back the same data, but upper-cased
# just send back the same data, but upper-cased
self.request.send(self.data.upper())
self.request.send(self.data.upper())
...
@@ -372,7 +372,7 @@ objects that simplify communication by providing the standard file interface)::
...
@@ -372,7 +372,7 @@ objects that simplify communication by providing the standard file interface)::
# self.rfile is a file-like object created by the handler;
# self.rfile is a file-like object created by the handler;
# we can now use e.g. readline() instead of raw recv() calls
# we can now use e.g. readline() instead of raw recv() calls
self.data = self.rfile.readline().strip()
self.data = self.rfile.readline().strip()
print("
%s wrote:" % self.client_address[0]
)
print("
{} wrote:".format(self.client_address[0])
)
print(self.data)
print(self.data)
# Likewise, self.wfile is a file-like object used to write back
# Likewise, self.wfile is a file-like object used to write back
# to the client
# to the client
...
@@ -395,16 +395,18 @@ This is the client side::
...
@@ -395,16 +395,18 @@ This is the client side::
# Create a socket (SOCK_STREAM means a TCP socket)
# Create a socket (SOCK_STREAM means a TCP socket)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
# Connect to server and send data
# Connect to server and send data
sock.connect((HOST, PORT))
sock.connect((HOST, PORT))
sock.send(bytes(data + "\n","utf
8"))
sock.send(bytes(data + "\n", "utf-
8"))
# Receive data from the server and shut down
# Receive data from the server and shut down
received = sock.recv(1024)
received = str(sock.recv(1024), "utf-8")
finally:
sock.close()
sock.close()
print("Sent:
%s" % data
)
print("Sent:
{}".format(data)
)
print("Received:
%s" % received
)
print("Received:
{}".format(received)
)
The output of the example should look something like this:
The output of the example should look something like this:
...
@@ -421,10 +423,10 @@ Client::
...
@@ -421,10 +423,10 @@ Client::
$ python TCPClient.py hello world with TCP
$ python TCPClient.py hello world with TCP
Sent: hello world with TCP
Sent: hello world with TCP
Received:
b'HELLO WORLD WITH TCP'
Received:
HELLO WORLD WITH TCP
$ python TCPClient.py python is nice
$ python TCPClient.py python is nice
Sent: python is nice
Sent: python is nice
Received:
b'PYTHON IS NICE'
Received:
PYTHON IS NICE
:class:`socketserver.UDPServer` Example
:class:`socketserver.UDPServer` Example
...
@@ -445,7 +447,7 @@ This is the server side::
...
@@ -445,7 +447,7 @@ This is the server side::
def handle(self):
def handle(self):
data = self.request[0].strip()
data = self.request[0].strip()
socket = self.request[1]
socket = self.request[1]
print("
%s wrote:" % self.client_address[0]
)
print("
{} wrote:".format(self.client_address[0])
)
print(data)
print(data)
socket.sendto(data.upper(), self.client_address)
socket.sendto(data.upper(), self.client_address)
...
@@ -467,11 +469,11 @@ This is the client side::
...
@@ -467,11 +469,11 @@ This is the client side::
# As you can see, there is no connect() call; UDP has no connections.
# As you can see, there is no connect() call; UDP has no connections.
# Instead, data is directly sent to the recipient via sendto().
# Instead, data is directly sent to the recipient via sendto().
sock.sendto(bytes(data + "\n",
"utf
8"), (HOST, PORT))
sock.sendto(bytes(data + "\n",
"utf-
8"), (HOST, PORT))
received = s
ock.recv(1024
)
received = s
tr(sock.recv(1024), "utf-8"
)
print("Sent:
%s" % data
)
print("Sent:
{}".format(data)
)
print("Received:
%s" % received
)
print("Received:
{}".format(received)
)
The output of the example should look exactly like for the TCP server example.
The output of the example should look exactly like for the TCP server example.
...
@@ -491,9 +493,9 @@ An example for the :class:`ThreadingMixIn` class::
...
@@ -491,9 +493,9 @@ An example for the :class:`ThreadingMixIn` class::
class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):
class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):
def handle(self):
def handle(self):
data = s
elf.request.recv(1024
)
data = s
tr(self.request.recv(1024), 'ascii'
)
cur_thread = threading.current_thread()
cur_thread = threading.current_thread()
response = bytes("
%s: %s" % (cur_thread.getName(), data),
'ascii')
response = bytes("
{}: {}".format(cur_thread.name, data),
'ascii')
self.request.send(response)
self.request.send(response)
class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
...
@@ -502,9 +504,11 @@ An example for the :class:`ThreadingMixIn` class::
...
@@ -502,9 +504,11 @@ An example for the :class:`ThreadingMixIn` class::
def client(ip, port, message):
def client(ip, port, message):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((ip, port))
sock.connect((ip, port))
sock.send(message)
try:
response = sock.recv(1024)
sock.send(bytes(message, 'ascii'))
print("Received: %s" % response)
response = str(sock.recv(1024), 'ascii')
print("Received: {}".format(response))
finally:
sock.close()
sock.close()
if __name__ == "__main__":
if __name__ == "__main__":
...
@@ -518,13 +522,13 @@ An example for the :class:`ThreadingMixIn` class::
...
@@ -518,13 +522,13 @@ An example for the :class:`ThreadingMixIn` class::
# more thread for each request
# more thread for each request
server_thread = threading.Thread(target=server.serve_forever)
server_thread = threading.Thread(target=server.serve_forever)
# Exit the server thread when the main thread terminates
# Exit the server thread when the main thread terminates
server_thread.
setDaemon(True)
server_thread.
daemon = True
server_thread.start()
server_thread.start()
print("Server loop running in thread:", server_thread.name)
print("Server loop running in thread:", server_thread.name)
client(ip, port,
b
"Hello World 1")
client(ip, port, "Hello World 1")
client(ip, port,
b
"Hello World 2")
client(ip, port, "Hello World 2")
client(ip, port,
b
"Hello World 3")
client(ip, port, "Hello World 3")
server.shutdown()
server.shutdown()
...
@@ -533,9 +537,9 @@ The output of the example should look something like this::
...
@@ -533,9 +537,9 @@ The output of the example should look something like this::
$ python ThreadedTCPServer.py
$ python ThreadedTCPServer.py
Server loop running in thread: Thread-1
Server loop running in thread: Thread-1
Received:
b"Thread-2: b'Hello World 1'"
Received:
Thread-2: Hello World 1
Received:
b"Thread-3: b'Hello World 2'"
Received:
Thread-3: Hello World 2
Received:
b"Thread-4: b'Hello World 3'"
Received:
Thread-4: Hello World 3
The :class:`ForkingMixIn` class is used in the same way, except that the server
The :class:`ForkingMixIn` class is used in the same way, except that the server
...
...
Misc/NEWS
View file @
023611f3
...
@@ -190,6 +190,12 @@ Extension Modules
...
@@ -190,6 +190,12 @@ Extension Modules
- Issue #12950: Fix passing file descriptors in multiprocessing, under
- Issue #12950: Fix passing file descriptors in multiprocessing, under
OpenIndiana/Illumos.
OpenIndiana/Illumos.
Documentation
-------------
- Issue #13141: Demonstrate recommended style for socketserver examples.
What's New in Python 3.2.2?
What's New in Python 3.2.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