• 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

settimeout(3) in python code of Web Server examples, after the .accept() command.

Q&A Forum › settimeout(3) in python code of Web Server examples, after the .accept() command.
0 Vote Up Vote Down
Joe Margevicius asked 4 years ago

In the python code for web server for an LED button, these 2 commands appear in main(): conn, addr = s.accept() and conn.settimeout(3.0). I understand that the accept() is blocking and the code hangs there until a client appears. What is the purpose of the settimeout() and what does it do (presumeably after a client appears) and the program advances to this line?? (it appears in some Web Servers examples found on github for the book, but not others (e.g. the DHT22 one). The code presented in the book doesn’t have these settimeout() lines after the accept()

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

Hi.
That is because Google Chrome sometimes makes an extra request and lefts the connection open. While the connection is opened, no other client can connect. So, if the connection is not closed after three seconds, it means its probably one of those problematic connections, so we need to close it manually.
Regards,
Sara

0 Vote Up Vote Down
Joe Margevicius answered 4 years ago

Thanks Sara, that’s helpful.  … I’m comparing the C-code example in the “Learn ESP32 with Arduino IDE” book and the python-code example in the “Micropython ESP32 and ESP82665” book, for the Webserver/button example. I see that the C-code is more than double in size (… why would I use C if python is so much more compact and readable?), but it looks like, with the C-code’s while(client.connected and “time checking”) statement this does the same thing as the settimeout(3) python statement… correct?    ….

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

Hi.
Yes, that’s correct.
 
I think it depends on the programming language you like more and the resources available for each language.
For example, there are many more resources for C with the ESP32 than MicroPython (libraries, functions, forums, and tutorials on the internet).
 
Programming the ESP32 with Arduino IDe using the Arduino core provides much more flexibility and customization in terms of programming and what is possible to do.
 
On the other side, using MicroPython is way simpler for someone new to programming or someone already used to programming in Python. There are simpler things to program in MicroPython than in C (for example, controlling an addressable RGB LED strip). But, in some scenarios, it is much more limited.
 
I hope this answers your question.
Regards,
Sara

0 Vote Up Vote Down
Joe Margevicius answered 4 years ago

Thanks Sara … very helpful as I navigate using python, c or javascript in webservers

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

Great!
I’ll mark this issue as resolved.
If you need further help, you just need to open a new question in our forum.
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.