own IP address?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
jcw
Posts: 37
Joined: Sat Dec 21, 2019 12:08 pm
Location: CET/CEST

own IP address?

Post by jcw » Fri Jun 05, 2020 7:33 pm

I need to report "my" IP address to a connected client. The server is bound to interface "0.0.0.0", i.e. any.
The socket.accept() call returns the address of the other end, i.e. the client's IP address.

In general, "my" IP address is ill-defined, i.e. there can be more than one interface.
But once there is a connection, is there is some way to find out which interface it's bound to?
Jean-Claude Wippler

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: own IP address?

Post by Roberthh » Fri Jun 05, 2020 8:42 pm

wlan.ifconfig()?

User avatar
jcw
Posts: 37
Joined: Sat Dec 21, 2019 12:08 pm
Location: CET/CEST

Re: own IP address?

Post by jcw » Fri Jun 05, 2020 8:59 pm

Whee, could it be that simple? Not sure - I'm trying this on unix, there's no network module, is there?

UPDATE: Aha, the IP address is not needed after all (I am writing a little FTP server, but PASV also works with a "0.0.0.0" reply).

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: own IP address?

Post by Roberthh » Sat Jun 06, 2020 5:41 am

If you want to look at another implementation for comparison, you may look here: https://github.com/robert-hh/FTP-Server ... 2-and-PYBD, file ftp.py. It is based on work from Christopher Popp and Paul Sokolovsky.

User avatar
jcw
Posts: 37
Joined: Sat Dec 21, 2019 12:08 pm
Location: CET/CEST

Re: own IP address?

Post by jcw » Sat Jun 06, 2020 7:45 am

Hey, thanks ... nice! Now I see where your ifconfig suggestion comes from.

FWIW, my freshly brewed implementation is here: https://git.jeelabs.org/jcw/pythonx/src ... master/ftp

It uses uasyncio to avoid blocking on new clients connecting and on waiting for a new command.
It also turns out to be very slow ... I suspect due to the underlying blocking flash file system I/O.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: own IP address?

Post by Roberthh » Sat Jun 06, 2020 11:32 am

Very elegant and concise code.

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: own IP address?

Post by kevinkk525 » Sat Jun 06, 2020 12:52 pm

looks nice. Might even consider using it myself if it was published on github so I can make it a submodule in my project.
So far Robert's callback driven ftp server has served me well on the esp32.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

User avatar
jcw
Posts: 37
Joined: Sat Dec 21, 2019 12:08 pm
Location: CET/CEST

Re: own IP address?

Post by jcw » Mon Jun 08, 2020 11:40 am

Thanks. Kevin, it's very easy to integrate any git repository into your own setup, e.g.

Code: Select all

git submodule add https://git.jeelabs.org/jcw/pythonx jcw-pythonx
Perhaps not widely known due to GitHub's popularity, but git itself is a distributed version control system.

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: own IP address?

Post by kevinkk525 » Mon Jun 08, 2020 3:24 pm

oh, thanks. I might try that when I continue programming.
Never tried integrating a git repository that wasn't hosted on github itself.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

Post Reply