]> vault307.fbx.one Git - esp32Cam.git/blob - espcamserver.py
esp32Camera webserver
[esp32Cam.git] / espcamserver.py
1 import network, socket, time
2 from secrets import secrets
3
4 ssid=secrets['ssid']
5 password = secrets['pw']
6
7 def connect():
8 wlan=network.WLAN(network.STA_IF)
9 wlan.active(True)
10 wlan.connect(ssid,password)
11 while wlan.isconnected() == False:
12 print('Waiting for connection...')
13 time.sleep(1)
14 ip=wlan.ifconfig()[0]
15 print(f'Connected on {ip}')
16 return ip
17
18 def open_socket(ip):
19 address=(ip,80)
20 connection=socket.socket()
21 connection.bind(address)
22 connection.listen(1)
23 return connection
24
25 def get_html(html_name):
26 with open(html_name, 'r') as file:
27 html=file.read()
28 return html
29
30 def serve(connection):
31
32 while True:
33 client=connection.accept()[0]
34 request=client.recv(1024)
35 request=str(request)
36 try:
37 response=get_html('index.html')
38 request=request.split()[1]
39 except IndexError:
40 pass
41
42 client.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n')
43 client.send(response)
44 client.close()
45
46 try:
47 ip=connect()
48 connection=open_socket(ip)
49 serve(connection)
50 except KeyboardInterrupt:
51 pass