Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 37 additions & 36 deletions Doc/library/socket.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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)

::

Expand Down Expand Up @@ -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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same there. # the public network interface should start at the same level of indentation as import socket (line 1680)

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
Expand Down Expand Up @@ -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::
Expand Down