I'm ultimately trying to test a UDP client, and want to make sure that it works when sending data not through the loopback interface, to avoid any subtle issues this introduces, such as differences in checksum validation (Bad UDP checksum has no effect: why?).
However, even when sending data to the result of socket.gethostbyname(socket.gethostname())
, which is not 127.0.0.1
, then according to Wireshark, the data seems to go via the loopback interface.
The below program sends and receives b'somedata'
successfully, and has the below capture from Wireshark.
import asyncio
import socket
async def server():
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
sock.setblocking(False)
sock.bind(('', 4567))
data = await loop.sock_recv(sock, 512)
print('Received', data)
async def main():
local_ip = socket.gethostbyname(socket.gethostname())
print('Local IP', local_ip) # Outputs 192.168.0.34
asyncio.ensure_future(server())
await asyncio.sleep(0)
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
sock.setblocking(False)
sock.connect((local_ip, 4567))
await loop.sock_sendall(sock, b'somedata')
await asyncio.sleep(1)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
How can I send data from a client running locally, to a server running locally, but avoiding the loopback interface and actually sending data out into the network?
Ideally answers would be applicable to both Linux and macOS.