Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- from time import sleep
- from os import stat
- from random import randrange as rng # using this for random cache time between 1 and 2 weeks
- from gc import mem_free as fram
- from gc import collect as clearRAM
- import network
- import socket
- def file_exists(filename):
- try:
- return (stat(filename)[0] & 0x4000) == 0
- except OSError:
- return False
- ssid="Your's WiFi"
- password="12345678"
- # WOW much security; This is fine...
- wlan = network.WLAN(network.STA_IF)
- wlan.active(True)
- wlan.connect(ssid, password)
- max_wait = 10
- while max_wait > 0:
- if wlan.status() < 0 or wlan.status() >= 3:
- break
- max_wait -= 1
- print('Waiting for connection...')
- sleep(1)
- if wlan.status() != 3:
- raise RuntimeError('network connection failed')
- else:
- print('connected')
- status = wlan.ifconfig()
- print( 'ip = ' + status[0] )
- addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
- s = socket.socket()
- s.bind(addr)
- s.listen(1)
- print('Listening on', addr,'\n\n')
- while True:
- try:
- cl, addr = s.accept()
- print('Client connected from', addr)
- request = cl.recv(1024)
- request = str(request)
- request=request.split("\\r\\n")[0].split(' ')
- if len(request) > 0:
- if request[0]=="b'GET":
- if request[1] == "/":
- request[1] = "/index.html"
- if request[1] == "/favicon.ico":
- request[1] = "/favicon.png"
- file='www'+request[1]
- if file_exists(file):
- header='HTTP/1.0 200 OK\r\nContent-type: '
- if request[1].find('.html') > 0:
- header+='text/html'
- elif request[1].find('.css') > 0:
- header+='text/css'
- elif request[1].find('.js') > 0:
- header+='application/javascript'
- elif request[1].find('.webp') > 0:
- header+='image/webp'
- elif request[1].find('.gif') > 0:
- header+='image/gif'
- elif request[1].find('.png') > 0:
- header+='image/png'
- elif request[1].find('.jpg') > 0:
- header+='image/jpeg'
- elif request[1].find('.gif') > 0:
- header+='image/gif'
- else:
- print('Not Implemented:', file,'\n')
- cl.send(header+'text/plain\r\n\r\nFile type not impliemted')
- cl.close()
- print('Closed connection\n')
- continue
- end=stat(file)[6]
- print('Open',end,':',file,'with', fram())
- if(end*1.1 < fram()): # end*1.1 is to be sure we are not completely out of memory, in theory you do not need the *1.1
- cl.send(header+'\r\nContent-Language: pt-BR\r\nCache-Control: max-age='+str(rng(604800,1209600))+'\r\n')
- with open(file, 'rb') as f:
- cl.send('Content-Length:'+str(end))
- f=f.read()
- cl.send('\r\n\r\n')
- cl.sendall(f)
- else:
- print("Low Ram: Offload",file,"to alt server")
- # this is gonna be a issue if it happens to index.html
- cl.send('HTTP/1.0 301 OK\r\nLocation: http://10.0.0.25:8080/thermostat/'+file[4:])
- elif request[1].find('.json') >0:
- print('Emulate:',file)
- if file[0:13] == "www/temp.json":
- json='{"temp":"20000","tube1":0,"tube2":1,"relay1":0,"relay2":1}'
- elif file[0:15] == "www/config.json":
- json='{"target": 72.5, "format": "F", "auxon": 1, "auxenabled": 1, "enabled": 1, "auxoff": 2, "trigger": 1, "sunset": 30, "night": 0, "day": 0, "sunrise": 90}'
- elif file[0:12] == "www/sun.json":
- json='{"rise":{"time":"6:59 AM","hour":6,"minute":59,"stamp":1664103540},"set":{"time":"6:56 PM","hour":18,"minute":56,"stamp":1664146560}}'
- elif file[0:13] == "www/cron.json":
- json='[{"enable":false,"days":[6,0],"time":{"start":{"h":22,"m":30},"end":{"h":23,"m":59}},"offset":-0.45,"name":"1. Chad wants cooler for sleep on weekend"},{"enable":false,"days":[1,0],"time":{"start":{"h":0,"m":0},"end":{"h":3,"m":0}},"offset":-0.45,"name":"2. Chad wants cooler for sleep on weekend"},{"enable":true,"days":[1,2,3,4,5,6,0],"time":{"start":{"h":4,"m":0},"end":{"h":10,"m":0}},"offset":-0.5,"name":"Lower temp in early morning"}]'
- else:
- json='{"Error":"File not implemented, typo?"}'
- cl.send('HTTP/1.0 200 OK\r\nContent-type: application/json\r\nContent-Length: '+str(len(json))+'\r\nCache-Control: no-cache\r\n\r\n'+json)
- else:
- print('404 Error',file)
- cl.send('HTTP/1.0 404 OK\r\nContent-type: text/html\r\n\r\n<html><head><title>404 Error</title></head><body>404 File not found</body></html>')
- elif request[0]=="b'POST":
- print('Not Implemented: todo')
- else:
- print('Say what?:',request)
- else:
- print('Say what?:',request)
- cl.close()
- print('Closed connection\n')
- clearRAM()
- except OSError as e:
- cl.close()
- print('Connection closed; Error:',e,'\n')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement