Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(6)

Side by Side Diff: Lib/socketserver.py

Issue 26404: socketserver context manager
Patch Set: Created 3 years, 11 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 """Generic socket server classes. 1 """Generic socket server classes.
2 2
3 This module tries to capture the various aspects of defining a server: 3 This module tries to capture the various aspects of defining a server:
4 4
5 For socket-based servers: 5 For socket-based servers:
6 6
7 - address family: 7 - address family:
8 - AF_INET{,6}: IP (Internet Protocol) sockets (default) 8 - AF_INET{,6}: IP (Internet Protocol) sockets (default)
9 - AF_UNIX: Unix domain sockets 9 - AF_UNIX: Unix domain sockets
10 - others, e.g. AF_DECNET are conceivable (see <socket.h> 10 - others, e.g. AF_DECNET are conceivable (see <socket.h>
(...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after
370 370
371 The default is to print a traceback and continue. 371 The default is to print a traceback and continue.
372 372
373 """ 373 """
374 print('-'*40, file=sys.stderr) 374 print('-'*40, file=sys.stderr)
375 print('Exception happened during processing of request from', 375 print('Exception happened during processing of request from',
376 client_address, file=sys.stderr) 376 client_address, file=sys.stderr)
377 import traceback 377 import traceback
378 traceback.print_exc() 378 traceback.print_exc()
379 print('-'*40, file=sys.stderr) 379 print('-'*40, file=sys.stderr)
380
381 def __enter__(self):
382 return self
383
384 def __exit__(self, *args):
385 self.server_close()
380 386
381 387
382 class TCPServer(BaseServer): 388 class TCPServer(BaseServer):
383 389
384 """Base class for various socket-based server classes. 390 """Base class for various socket-based server classes.
385 391
386 Defaults to synchronous IP stream (i.e., TCP). 392 Defaults to synchronous IP stream (i.e., TCP).
387 393
388 Methods for the caller: 394 Methods for the caller:
389 395
(...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after
753 """Define self.rfile and self.wfile for datagram sockets.""" 759 """Define self.rfile and self.wfile for datagram sockets."""
754 760
755 def setup(self): 761 def setup(self):
756 from io import BytesIO 762 from io import BytesIO
757 self.packet, self.socket = self.request 763 self.packet, self.socket = self.request
758 self.rfile = BytesIO(self.packet) 764 self.rfile = BytesIO(self.packet)
759 self.wfile = BytesIO() 765 self.wfile = BytesIO()
760 766
761 def finish(self): 767 def finish(self):
762 self.socket.sendto(self.wfile.getvalue(), self.client_address) 768 self.socket.sendto(self.wfile.getvalue(), self.client_address)
OLDNEW
« Doc/whatsnew/3.6.rst ('K') | « Lib/http/server.py ('k') | Lib/test/test_socketserver.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+