Skip to content

Reorg code per request from orphan pr 69 #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed

Reorg code per request from orphan pr 69 #107

wants to merge 3 commits into from

Conversation

mytechnotalent
Copy link
Contributor

This PR deprecates PR 69 as a year has passed as I was tied up with other responsibilities. The code base has changed significantly so there was a large merge conflict with the prior work. This new PR takes all of the recommendations of @mscosti @brentru @jerryneedell into account. @kattni apology for taking so long.

Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for updating these changes!

Please only add CPython compatible APIs to socket. Looks like you want getpeername: https://docs.python.org/3/library/socket.html#socket.socket.getpeername

You cannot add it to the constructor because CPython doesn't have it.

@mytechnotalent
Copy link
Contributor Author

@tannewt there is no getpeername exposed in the Nina firmware https://github.com/adafruit/nina-fw/blob/master/main/CommandHandler.cpp. The getpeername from CPython exists here https://github.com/python/cpython/blob/e822e37946f27c09953bb5733acf3b07c2db690f/Modules/socketmodule.c#L3377.

Trying to add this API would be a major amount of work and to be honest without it following the convention of the rest of this lib I would not even know where to begin. Do you think this PR should simply be closed if it might not be a feature worth implementing?

@tannewt
Copy link
Member

tannewt commented Oct 6, 2020

@mytechnotalent I don't think you need getpeername exposed from Nina because you already have a way to get the remote ip. I think you can call that API from your getpeername implementation.

@mytechnotalent
Copy link
Contributor Author

mytechnotalent commented Oct 6, 2020

Can you provide more information? Perhaps an example? I am not following.

Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is a sketch of the suggested changes. I haven't tested it. My goal was to give an example of what I'd like to see.

Comment on lines +70 to +71
self._remote_ip = remote_ip
self._remote_port = remote_port
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self._remote_ip = remote_ip
self._remote_port = remote_port

resp = self._send_command_get_response(
_GET_REMOTE_DATA_CMD, self._socknum_ll, reply_params=2
)
return {"ip_addr": resp[0], "port": resp[1]}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return {"ip_addr": resp[0], "port": resp[1]}
return (resp[0], resp[1])

Comment on lines +205 to +214
@property
def remote_ip(self):
"""The remote ip"""
return self._remote_ip

@property
def remote_port(self):
"""The remote port"""
return self._remote_port

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@property
def remote_ip(self):
"""The remote ip"""
return self._remote_ip
@property
def remote_port(self):
"""The remote port"""
return self._remote_port
def getpeername(self):
ip, port = _the_interface.get_remote_data(self._socknum)
# Parse so that ip is a string and port is an int
# Second bullet here: https://docs.python.org/3/library/socket.html#socket-families
return (ip, port)

Comment on lines +61 to +62
remote_ip=None,
remote_port=None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
remote_ip=None,
remote_port=None,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh that makes sense @tannewt! I will work on these changes as soon as I get a second and will test and push an update. Thank you again.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No rush and no problem! Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tannewt I have implemented the changes. I wanted ask specifically how you would like this tested. Here is the result in the REPL.
>>> socket.socket. close connect connected read readline recv send settimeout write socknum available getpeername

I obviously cant call socket.socket.getpeername() so if you have a quick test I can code up I will begin testing so that I know what you are looking for specifically. Any simple example will do thanks!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd imagine you need to create a socket and connect before calling it. The new requests library can work with CPython sockets so that's probably the best place to start.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tannewt I created an access point called cool and then I connected my iPhone to it. It successfully connected however when I call socket.socket.getpeername() i get TypeError: function takes 1 positional arguments but 0 were given as it should be returning the peer ip of 192.168.4.2 as that is the iPhone and the server is 192.168.4.2.

Any suggestions on how to better check the getpeername call?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You actually need to connect a TCP socket and then call .getpeername() on it. The error is because you are calling it on the class, not an instance, and therefore no self is passed.

Why are you adding this overall? It doesn't seem like you have a use for it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great question @tannewt. Over a year ago when I did the original PR I had issues where I had the device set up as an Access Point and if I accidently logged in from multiple browsers on various machines it would crash.

This original PR was designed to check for that remote IP and if it was not 192.168.4.2 then don't connect. It was to add the ability to get this information if a developer wanted to implement functionality such as this.

I am ok with closing the PR however. Thoughts?

Copy link
Contributor Author

@mytechnotalent mytechnotalent Oct 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what I tried to do originally as well. I can't seem to even create a socket object here.

>>> import adafruit_esp32spi.adafruit_esp32spi_socket as socket
>>> s = socket.socket()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/lib/adafruit_esp32spi/adafruit_esp32spi_socket.py", line 62, in __init__
AttributeError: 'NoneType' object has no attribute 'get_socket'
>>> 

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the current ESP32-SPI implementation you need to call set_interface on the socket module: https://github.com/adafruit/Adafruit_CircuitPython_ESP32SPI/blob/master/adafruit_esp32spi/adafruit_esp32spi_socket.py#L24 I believe you pass it the ESP32SPI object.

I could see this PR being helpful but if no one is using it enough to test, then we could just close for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants