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

Side by Side Diff: Lib/socket.py

Issue 17561: Add socket.create_server_sock() convenience function
Patch Set: Created 6 years, 10 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
« no previous file with comments | « no previous file | Lib/test/test_socket.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Wrapper module for _socket, providing some additional facilities 1 # Wrapper module for _socket, providing some additional facilities
2 # implemented in Python. 2 # implemented in Python.
3 3
4 """\ 4 """\
5 This module provides socket operations and some related functions. 5 This module provides socket operations and some related functions.
6 On Unix, it supports IP (Internet Protocol) and Unix domain sockets. 6 On Unix, it supports IP (Internet Protocol) and Unix domain sockets.
7 On other systems, it only supports IP. Functions specific for a 7 On other systems, it only supports IP. Functions specific for a
8 socket are available as methods of the socket object. 8 socket are available as methods of the socket object.
9 9
10 Functions: 10 Functions:
(...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after
428 428
429 except error as _: 429 except error as _:
430 err = _ 430 err = _
431 if sock is not None: 431 if sock is not None:
432 sock.close() 432 sock.close()
433 433
434 if err is not None: 434 if err is not None:
435 raise err 435 raise err
436 else: 436 else:
437 raise error("getaddrinfo returns an empty list") 437 raise error("getaddrinfo returns an empty list")
438
439 def has_dual_stack():
440 """Return True if kernel allows creating a socket which is able to
441 listen for both IPv4 and IPv6 connections.
Charles-François Natali 2013/04/04 08:44:29 The term "dual stack" is a bit confusing: it just
442 """
443 if not has_ipv6 \
444 or not hasattr(_socket, 'AF_INET6') \
445 or not hasattr(_socket, 'IPV6_V6ONLY'):
Charles-François Natali 2013/04/04 08:44:29 I think PEP8 discourages line breaks using '\': pa
446 return False
447 try:
448 with socket(AF_INET6, SOCK_STREAM) as sock:
449 if not sock.getsockopt(IPPROTO_IPV6, IPV6_V6ONLY):
450 return True
451 else:
452 sock.setsockopt(IPPROTO_IPV6, IPV6_V6ONLY, False)
453 return True
454 except error:
455 return False
456
457 def create_server_sock(address,
458 reuse_addr=os.name == 'posix' and sys.platform != 'cygwin ',
459 queue_size=5,
460 dual_stack=has_dual_stack()):
Charles-François Natali 2013/04/04 08:44:29 Same thing here, I think it complicates the signat
461 """Convenience function which creates a TCP server bound to
462 *address* and return the socket object.
463
Charles-François Natali 2013/04/04 08:44:29 The *reuse_addr* argument is not documented in the
464 Internally it takes care of choosing the right address family
465 (IPv4 or IPv6) depending on the host specified in *address*.
466 If *host* is an empty string or None all interfaces are assumed.
467 If dual stack is supported by kernel the socket will be able to
468 listen for both IPv4 and IPv6 connections.
469
470 The returned socket can be used to accept() new connections as in:
471
472 >>> server = create_server_sock((None, 8000))
473 >>> while True:
474 ... sock, addr = server.accept()
475 ... # handle new sock connection
476 """
477 AF_INET6_ = getattr(_socket, "AF_INET6", 0)
Charles-François Natali 2013/04/04 08:44:29 It might be possible to use socket.has_ipv6.
478 host, port = address
479 if host == "":
480 # http://mail.python.org/pipermail/python-ideas/2013-March/019937.html
481 host = None
482 # if dual stack is supported and no host was specified assume '::'
483 # (IPv6 / all interfaces)
484 if host is None and dual_stack:
485 host = "::"
486 err = None
487 info = getaddrinfo(host, port, AF_UNSPEC, SOCK_STREAM, 0, AI_PASSIVE)
488 for res in info:
489 af, socktype, proto, canonname, sa = res
490 sock = None
491 try:
492 sock = socket(af, socktype, proto)
493 if reuse_addr:
494 sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
495 if af == AF_INET6_ and dual_stack:
496 if sock.getsockopt(IPPROTO_IPV6, IPV6_V6ONLY):
497 sock.setsockopt(IPPROTO_IPV6, IPV6_V6ONLY, False)
498 sock.bind(sa)
499 sock.listen(queue_size)
500 return sock
501 except error as _:
502 err = _
503 if sock is not None:
504 sock.close()
505
506 if err is not None:
507 raise err
508 else:
509 raise error("getaddrinfo returns an empty list")
OLDNEW
« no previous file with comments | « no previous file | Lib/test/test_socket.py » ('j') | no next file with comments »

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