title: Send UDP to local server, but not via loopback interface
body: <p>I'm ultimately trying to test a UDP client, and want to make sure that it works when sending data <em>not</em> through the loopback interface, to avoid any subtle issues this introduces, such as differences in checksum validation (<a href="https://stackoverflow.com/q/55797731/1319998">Bad UDP checksum has no effect: why?</a>).</p>
  
  <p>However, even when sending data to the result of <code>socket.gethostbyname(socket.gethostname())</code>, which is not <code>127.0.0.1</code>, then according to Wireshark, the data seems to go via the loopback interface.</p>
  
  <p>The below program sends and receives <code>b'somedata'</code> successfully, and has the below capture from Wireshark.</p>
  
  <pre class="lang-py prettyprint-override"><code>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()
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/WvGnR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WvGnR.png" alt="enter image description here"></a></p>
  
  <p>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?</p>
  
  <p>Ideally answers would be applicable to both Linux and macOS.</p>