Linux network namespaces

Hello All,

I was looking at Trio and wondering if there is any support to switch to various network namespaces asynchronously and make tcp/websocket/http connections.

Thanks,
Sid

Anyone? The usual way would be to :-

  • Enter networks namespace
  • Create socket so that we have reference when outside the namespace
  • Exit namespace

Now we can use this socket in default namespace. But i am finding it hard to see libraries such as trio.websockets or httpx that provide such an option.
I could potentially just call ip netns call and then invoke the trio.websockets or httpx GET/POST calls , but then that beats the purpose of async programming
Any pointers?
Thanks

I’m no expert at this, but I though that the usual way to use network namespaces (or any namespaces) is that you enter the namespace before you even run the process. As I understood it, that’s the whole point of namespaces. The Wikipedia article on namespaces and LWN article on network namespaces both talk about “processes are running within” the namespace.

So typical application processes, of the sort that are written with Trio etc., don’t need any special support. As far as those processes are concerned they’re just looking at the whole system (when, really, they’re just seeing the parts that are in the namespace).

I wouldn’t expect to see code for entering (or exiting) namespaces unless you’re writing some sort of Docker-like admin tool that sets up namespaces for application processes to run in.

Having said all that (and probably just proved my ignorance)…

If you know how to open a normal Python socket.socket in the way that you want (which could probably follow the three steps you suggested without blocking or doing any async wait), then you can convert that into a Trio socket with trio.socket.from_stdlib_socket. I’m not sure whether you can get from that to an httpx/websockets client - from a quick glance, it looks like yes for websockets but perhaps no for httpx.