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

Side by Side Diff: Lib/socketserver.py

Issue 26404: socketserver context manager
Patch Set: Created 4 years, 1 month 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 364 matching lines...) Expand 10 before | Expand all | Expand 10 after
375 375
376 The default is to print a traceback and continue. 376 The default is to print a traceback and continue.
377 377
378 """ 378 """
379 print('-'*40, file=sys.stderr) 379 print('-'*40, file=sys.stderr)
380 print('Exception happened during processing of request from', 380 print('Exception happened during processing of request from',
381 client_address, file=sys.stderr) 381 client_address, file=sys.stderr)
382 import traceback 382 import traceback
383 traceback.print_exc() 383 traceback.print_exc()
384 print('-'*40, file=sys.stderr) 384 print('-'*40, file=sys.stderr)
385
386 def __enter__(self):
387 return self
388
389 def __exit__(self, *args):
390 self.server_close()
385 391
386 392
387 class TCPServer(BaseServer): 393 class TCPServer(BaseServer):
388 394
389 """Base class for various socket-based server classes. 395 """Base class for various socket-based server classes.
390 396
391 Defaults to synchronous IP stream (i.e., TCP). 397 Defaults to synchronous IP stream (i.e., TCP).
392 398
393 Methods for the caller: 399 Methods for the caller:
394 400
(...skipping 366 matching lines...) Expand 10 before | Expand all | Expand 10 after
761 """Define self.rfile and self.wfile for datagram sockets.""" 767 """Define self.rfile and self.wfile for datagram sockets."""
762 768
763 def setup(self): 769 def setup(self):
764 from io import BytesIO 770 from io import BytesIO
765 self.packet, self.socket = self.request 771 self.packet, self.socket = self.request
766 self.rfile = BytesIO(self.packet) 772 self.rfile = BytesIO(self.packet)
767 self.wfile = BytesIO() 773 self.wfile = BytesIO()
768 774
769 def finish(self): 775 def finish(self):
770 self.socket.sendto(self.wfile.getvalue(), self.client_address) 776 self.socket.sendto(self.wfile.getvalue(), self.client_address)
OLDNEW

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