# Linux network namespaces

**URL:** <https://trio.discourse.group/t/linux-network-namespaces/512>\
**Category:** Uncategorized\
**Created:** [April 19, 2024, 6:10am UTC](https://trio.discourse.group/t/linux-network-namespaces/512 "2024-04-19T06:10:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rationale](https://avatars.discourse-cdn.com/v4/letter/r/82dd89/32.png) [@Rationale](https://trio.discourse.group/u/Rationale)\
**Post date:** [April 19, 2024, 6:10am UTC](https://trio.discourse.group/t/linux-network-namespaces/512/1 "2024-04-19T06:10:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Rationale](https://avatars.discourse-cdn.com/v4/letter/r/82dd89/32.png) [@Rationale](https://trio.discourse.group/u/Rationale)\
**Post date:** [April 22, 2024, 9:02pm UTC](https://trio.discourse.group/t/linux-network-namespaces/512/2 "2024-04-22T21:02:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![arthur-tacca](https://yyz2.discourse-cdn.com/free1/user_avatar/trio.discourse.group/arthur-tacca/32/324_2.png) [@arthur-tacca](https://trio.discourse.group/u/arthur-tacca)\
**Post date:** [May 13, 2024, 2:14pm UTC](https://trio.discourse.group/t/linux-network-namespaces/512/3 "2024-05-13T14:14:22Z")

</div>

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](https://en.wikipedia.org/wiki/Linux_namespaces) and [LWN article on network namespaces](https://lwn.net/Articles/580893/) 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.

---

<div class="post-metadata">

**Author:** ![arthur-tacca](https://yyz2.discourse-cdn.com/free1/user_avatar/trio.discourse.group/arthur-tacca/32/324_2.png) [@arthur-tacca](https://trio.discourse.group/u/arthur-tacca)\
**Post date:** [May 13, 2024, 2:26pm UTC](https://trio.discourse.group/t/linux-network-namespaces/512/4 "2024-05-13T14:26:46Z")

</div>

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

If you know how to open a normal Python [`socket.socket`](https://docs.python.org/3/library/socket.html) 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`](https://trio.readthedocs.io/en/stable/reference-io.html#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.
