-
-
Notifications
You must be signed in to change notification settings - Fork 34.8k
bpo-31739: Fixed socket documentation to use with statement for socket examples. #4181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1633,12 +1633,13 @@ sends traffic to the first one connected successfully. :: | |
| print('could not open socket') | ||
| sys.exit(1) | ||
| conn, addr = s.accept() | ||
| with conn: | ||
| print('Connected by', addr) | ||
| while True: | ||
| data = conn.recv(1024) | ||
| if not data: break | ||
| conn.send(data) | ||
| with s: | ||
| with conn: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since 3.1 and 2.7 you can put two context managers in one with s, conn statement. Or you could extend the with s bit to include the s.accept call above. |
||
| print('Connected by', addr) | ||
| while True: | ||
| data = conn.recv(1024) | ||
| if not data: break | ||
| conn.send(data) | ||
|
|
||
| :: | ||
|
|
||
|
|
@@ -1678,24 +1679,25 @@ the interface:: | |
|
|
||
| import socket | ||
|
|
||
| # the public network interface | ||
| HOST = socket.gethostbyname(socket.gethostname()) | ||
|
|
||
| # create a raw socket and bind it to the public interface | ||
| s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP) | ||
| s.bind((HOST, 0)) | ||
| # the public network interface | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same there. |
||
| HOST = socket.gethostbyname(socket.gethostname()) | ||
|
|
||
| # create a raw socket and bind it to the public interface | ||
| with socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP) as s: | ||
| s.bind((HOST, 0)) | ||
|
|
||
| # Include IP headers | ||
| s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1) | ||
| # Include IP headers | ||
| s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1) | ||
|
|
||
| # receive all packages | ||
| s.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON) | ||
| # receive all packages | ||
| s.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON) | ||
|
|
||
| # receive a package | ||
| print(s.recvfrom(65565)) | ||
| # receive a package | ||
| print(s.recvfrom(65565)) | ||
|
|
||
| # disabled promiscuous mode | ||
| s.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF) | ||
| # disabled promiscuous mode | ||
| s.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF) | ||
|
|
||
| The next example shows how to use the socket interface to communicate to a CAN | ||
| network using the raw socket protocol. To use CAN with the broadcast | ||
|
|
@@ -1729,23 +1731,22 @@ This last example might require special privileges:: | |
|
|
||
|
|
||
| # create a raw socket and bind it to the 'vcan0' interface | ||
| s = socket.socket(socket.AF_CAN, socket.SOCK_RAW, socket.CAN_RAW) | ||
| s.bind(('vcan0',)) | ||
|
|
||
| while True: | ||
| cf, addr = s.recvfrom(can_frame_size) | ||
|
|
||
| print('Received: can_id=%x, can_dlc=%x, data=%s' % dissect_can_frame(cf)) | ||
|
|
||
| try: | ||
| s.send(cf) | ||
| except OSError: | ||
| print('Error sending CAN frame') | ||
|
|
||
| try: | ||
| s.send(build_can_frame(0x01, b'\x01\x02\x03')) | ||
| except OSError: | ||
| print('Error sending CAN frame') | ||
| with socket.socket(socket.AF_CAN, socket.SOCK_RAW, socket.CAN_RAW) as s: | ||
| s.bind(('vcan0',)) | ||
| while True: | ||
| cf, addr = s.recvfrom(can_frame_size) | ||
|
|
||
| print('Received: can_id=%x, can_dlc=%x, data=%s' % dissect_can_frame(cf)) | ||
|
|
||
| try: | ||
| s.send(cf) | ||
| except OSError: | ||
| print('Error sending CAN frame') | ||
|
|
||
| try: | ||
| s.send(build_can_frame(0x01, b'\x01\x02\x03')) | ||
| except OSError: | ||
| print('Error sending CAN frame') | ||
|
|
||
| Running an example several times with too small delay between executions, could | ||
| lead to this error:: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indentation is a bit inconsistent here. Please 4-space indentation.