Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- this is the output on the serial terminal
- Content = b'POST /test HTTP/1.1\r\nHost: 192.168.0.29\r\nConnection: keep-alive\r\nContent-Length: 101\r\nCache-Control: max-age=0\r\nOrigin: http://192.168.0.29\r\nUpgrade-Insecure-Requests: 1\r\nContent-Type: application/x-www-form-urlencoded\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3\r\nReferer: http://192.168.0.29/test\r\nAccept-Encoding: gzip, d'
- and this is the modified code (it probably looks like a mess):
- import gc
- import os
- value = 0
- string = "HelloWorld"
- firstNum = 0.0
- secNum = 0.0
- calcNum = 0.0
- calcError = ""
- extraHTML = '<meta name="viewport" content="width=device-width, initial-scale=1"><link rel="icon" href="data:,"><style>html{font-family: Helvetica; display:inline-block; margin: 0px auto; text-align: center;} h1{color: #0F3376; padding: 2vh;}p{font-size: 1.5rem;}.button{display: inline-block; background-color: #e7bd3b; border: none; border-radius: 4px; color: white; padding: 16px 40px; text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;} .button2{background-color: #4286f4;}</style>'
- _hextobyte_cache = None
- def unquote(string):
- """unquote('abc%20def') -> b'abc def'."""
- global _hextobyte_cache
- # Note: strings are encoded as UTF-8. This is only an issue if it contains
- # unescaped non-ASCII characters, which URIs should not.
- if not string:
- return b''
- if isinstance(string, str):
- string = string.encode('utf-8')
- bits = string.split(b'%')
- if len(bits) == 1:
- return string
- res = [bits[0]]
- append = res.append
- # Build cache for hex to char mapping on-the-fly only for codes
- # that are actually used
- if _hextobyte_cache is None:
- _hextobyte_cache = {}
- for item in bits[1:]:
- try:
- code = item[:2]
- char = _hextobyte_cache.get(code)
- if char is None:
- char = _hextobyte_cache[code] = bytes([int(code, 16)])
- append(char)
- append(item[2:])
- except KeyError:
- append(b'%')
- append(item)
- return b''.join(res)
- def indexpage():
- def df():
- s = os.statvfs('//')
- return ('{0} MB'.format((s[0]*s[3])/1048576))
- def free(full=False):
- F = gc.mem_free()
- A = gc.mem_alloc()
- T = F+A
- P = '{0:.2f}%'.format(F/T*100)
- if not full: return P
- else : return ('Total:{0} Free:{1} ({2})'.format(T,F,P))
- ramrom = "Free Space: " + df() + " Memory Usage: " + free()
- html = '''<html>
- <head>
- <title>ESP Web Server</title>''' + extraHTML +'''</head>
- <body>
- <h1>ESP Web Server on Micropython</h1>
- <h6>Original code from <a href="https://randomnerdtutorials.com" target="_blank">randomnerdtutorials.com</a>
- </h6>
- <h3>
- <a href="/test">Goto /test</a>
- </h3>
- <p>Memory usage: <strong>''' + ramrom + '''</strong>
- </p>
- <p>Value: <strong>''' + str(value) + '''</strong>
- </p>
- <p>
- <a href="/?valueadd">
- <button class="button">increase</button>
- </a>
- </p>
- <p>
- <a href="/?valuedec">
- <button class="button button2">Decrease</button>
- </a>
- </p>
- <p>String = <strong>''' + string + '''</strong>
- </p>
- <input id="txt" type="text" value="''' + string +'''">
- <p>
- <a onclick="location.href=\'/?string=\' + document.getElementById(\'txt\').value + \'EndOfStr\';">
- <button class="button button2">setstring</button>
- </a>
- </p>
- </body>
- </html>'''
- return html
- def secondpage():
- try:
- calcNum = firstNum * secNum
- except ValueError:
- calcNum = " ValueError Occured"
- html2 = '''<html>
- <head>
- <title>FormTest</title>' + extraHTML +'</head>
- <body>
- <h1>Forms Test page gengs</h1>
- <h3>
- <a href="/">Goto /</a>
- </h3>
- <h3>
- </strong>First Number: ''' + str(firstNum) + ''' </strong>
- </h3>
- <h3>
- </strong>Second Number: ''' + str(secNum) + ''' </strong>
- </h3>
- <h3>
- </strong>Resulting Number: ''' + str(calcNum) + calcError + ''' </strong>
- </h3>
- </br>
- <form action="/test">First Number:<br>
- <input type="text" name="firstnum" value="420">
- <br>Multiplied by<br>Second Number:<br>
- <input type="text" name="secondnum" value="420">
- <br>
- <br>
- <input type="submit" value="Submit">
- </form>
- <br>
- <form action="/test" method="POST">
- First name: <input type="text" name="fname"><br>
- Last name: <input type="text" name="lname"><br>
- <input type="submit" value="Submit">
- </form>
- </body>
- </html>'''
- return html2
- 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)
- request = str(request)
- print('Content = %s' % request)
- valueadd = request.find('/?valueadd')
- valuedec = request.find('/?valuedec')
- chstring = request.find('/?string=')
- strend = request.find('EndOfStr')
- getFirstNum = request.find('?firstnum=')
- getFirstNumEnd = request.find('&secondnum')
- getSecondNum = request.find('&secondnum=')
- getRequestEnd = request.find(" HTTP/")
- if valueadd == 6:
- value += 1
- if valuedec == 6:
- value -= 1
- if chstring == 6:
- string = request[15:int(strend)]
- if getFirstNum == 11:
- try:
- calcError = ""
- diff = getSecondNum + 11
- firstNum = float(request[21:getFirstNumEnd])
- secNum = float(request[diff:getRequestEnd])
- except ValueError:
- calcError = " ValueError Occured"
- if request[6:11] == "/test":
- response = secondpage()
- else:
- response = indexpage()
- 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()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement