• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

Micropython basic questions

Q&A Forum › Micropython basic questions
0 Vote Up Vote Down
José Magalhães asked 5 years ago

Hi there!

I’m trying to learn Micropython from your book: MicroPython_Programming_with_ESP32_and_ESP8266_V1_2. I’d like some questions cleared if possible:

. page 159, line “conn, addr = s.accept() “
I believe you are creating two variables: conn and addr, the first being a new socket for connecting to the client, however I don’t understand how it is populated. The addr variable I believe to be the ip address of the client returned by s.accept()

.page160, line: “ print(‘Got a connection from %s’ % str(addr))
I get the client ip address printed in the terminal followed by a , and another number
the %s is a string defined by %str(addr) portion? right?
what is the other number printed on the terminal after the ip address?

Thanks in advance, sorry for my “noobness”

3 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 5 years ago

Hi.

That method returns two variables. Here’s the description from the documentation:

socket.accept()

“Accept a connection. The socket must be bound to an address and listening for connections. The return value is a pair (conn, address) where conn is a new socket object usable to send and receive data on the connection, and address is the address bound to the socket on the other end of the connection.” https://docs.micropython.org/en/latest/library/usocket.html#usocket.socket.accept

As for the other question. I’m not really sure what the number that follows the IP address means. Is it different for each connection, right? I think it is a unique number used to identify the connection.

I’m sorry that I can’t help much.

Regards,
Sara

0 Vote Up Vote Down
José Magalhães answered 5 years ago

You have been most helpful. Thanks a lot.
By the way the %s is a string defined by %str(addr) portion? Or is it related to the socket variable s?

0 Vote Up Vote Down
Sara Santos Staff answered 5 years ago

Hi again.
Yes. The %s is the string you get from the %str(addr).
The %s is like a placeholder for a string. At the end of the print() function, after the %, we insert what we want to replace the placeholder with.
I hope this is clear.
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.