• 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

undefined name 'socket' error

Q&A Forum › Category: ESP8266 › undefined name 'socket' error
0 Vote Up Vote Down
William Spedding asked 2 years ago

In the following eBook (great book by the way!):
“Welcome to MicroPython Programming with ESP32 and ESP8266” in web servers page 203
I checked the boot.py with syntax and checked ok.
I checked Main.py with syntax check, and attempts to download and run, brings up the following error
“MicroPython v1.20.0 on 2023-04-26; ESP module with ESP8266
Type “help()” for more information.
>>>
main.py:8: undefined name ‘socket’
main.py:8: undefined name ‘socket’
main.py:8: undefined name ‘socket’
syntax finish.”
The code in line 8 reads
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Your advise greatly appreciated
Thanking you

Question Tags: Sparky
5 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 2 years ago

Hi.
Try cutting the whole lines of code on boot.py and paste them to the start of main.py.
 
let me know if this solves the issue.
 
Regards,
Sara

0 Vote Up Vote Down
William Spedding answered 2 years ago

Hello Sara
Thanking you
Pasting boot.py into main.py syntax check runs ok without any errors being flagged.
DownloadAndRun produced the following errors:

Ready to download this file,please wait!
………
download ok
exec(open(‘main.py’).read(),globals())
Traceback (most recent call last):
File “”, line 1, in
File “”, line 7, in
NameError: name ‘esp’ isn’t defined

So we have progressed - a little
Thanking you for further help

try:
import usocket as socket
except:
import socket
import network
import esp
esp.osdebug(None) #error appears to occur on this line
import gc
gc.collect()
ssid = 'REPLACE_WITH_YOUR_SSID'
password = 'REPLACE_WITH_YOUR_PASSWORD'
station = network.WLAN(network.STA_IF)
station.active(True)
station.connect(ssid, password)
while station.isconnected() == False:
pass
print('Connection successful')
print(station.ifconfig())
# Complete project details at https://RandomNerdTutorials.com

def web_page():
html = """
Hello, World!"""
return html

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 80))
s.listen(5)

while True:
conn, addr = s.accept()
print('Got a connection from %s' % str(addr))
request = conn.recv(1024)
print('Content = %s' % str(request))
response = web_page()
conn.send('HTTP/1.1 200 OK\n')
conn.send('Content-Type: text/html\n')
conn.send('Connection: close\n\n')
conn.sendall(response)
conn.close()

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

I think you can remove the line that is causing the issue.
However, I don’t know why that error is caused 😐
Try having those lines on the boot.py and the rest of the code in main.py.
This lines would go on boot.py:
import esp
esp.osdebug(None) #error appears to occur on this line
import gc
gc.collect()
 
Let me know if this solves the problem.
Regards,
Sara
 

0 Vote Up Vote Down
William Spedding answered 2 years ago

Hello Sara Thanking you heaps! Finally got the py program to work, with a bit of juggling
Boot.Py is now:

import esp
esp.osdebug(None) #error appears to occur on this line
import gc
gc.collect()

Main.py is now

#try:
import usocket as socket
#except:
#import socket
import network
import esp
#esp.osdebug(None)
import gc
gc.collect()
ssid = 'Telstra7E9306'
password = 's5ynjh2rdf'
station = network.WLAN(network.STA_IF)
station.active(True)
station.connect(ssid, password)
while station.isconnected() == False:
pass
print('Connection successful')
print(station.ifconfig())
# Complete project details at https://RandomNerdTutorials.com

def web_page():
html = """
Hello William!"""
return html

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 80))
s.listen(5)

while True:
conn, addr = s.accept()
print('Got a connection from %s' % str(addr))
request = conn.recv(1024)
print('Content = %s' % str(request))
response = web_page()
conn.send('HTTP/1.1 200 OK\n')
conn.send('Content-Type: text/html\n')
conn.send('Connection: close\n\n')
conn.sendall(response)
conn.close(

Please note the “Hashed out” lines where errors had occurred.
Many thanks again and now onto the next task of getting the “on-Off” LEDs working!

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

Great.
I don’t understand why the other lines of code were not running before the main.py file… But I’m glad we’ve found a solution.
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

  • [eBook Updated] Learn Raspberry Pi Pico/Pico W with MicroPython eBook – Version 1.2 May 26, 2025
  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition 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.