Answer the question
In order to leave comments, you need to log in
How to write script in python?
Hello everyone, how to rewrite Flask Python 2.7 code on SimpleHttServer? the fact is that we need the capabilities of python 3, and I can’t install a flask with python 3 due to problems with the OS, which would make it easier to rewrite on a simple server with python 3 support. Here is the source code
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from flask import Flask, render_template, request
app = Flask(__name__)
app.config.from_object(__name__)
@app.route('/')
def welcome():
return render_template('form.html')
@app.route('/result', methods=['POST'])
def result():
var_1 = request.form.get("var_1", type=int)
var_2 = request.form.get("var_2", type=int)
operation = request.form.get("operation")
operation2 = request.form.get("operation2")
operation3 = request.form.get("operation3")
if(operation == u'Q (теплота в Дж)' and operation2 == u"q (Электрический заряд в Кл)" and operation3 == u"C (емкость в фарад)"):
result = var_1**2 / 2*var_2
else:
result = 'INVALID CHOICE'
entry = result
return render_template('result.html', entry=entry)
if __name__ == '__main__':
app.run(debug=True)
from http.server import HTTPServer, BaseHTTPRequestHandler
class Serv(BaseHTTPRequestHandler):
def do_GET(self):
if self.path == '/':
self.path = '/index.html'
try:
file_to_open = open(self.path[1:]).read()
self.send_response(200)
except:
file_to_open = "File not found"
self.send_response(404)
self.end_headers()
self.wfile.write(bytes(file_to_open, 'utf-8'))
httpd = HTTPServer(('localhost', 8080), Serv)
httpd.serve_forever()
Answer the question
In order to leave comments, you need to log in
Here is an example code on the knee, it displays the sum of two numbers, but it can break at any time:
#!/usr/bin/env python3
from http.server import BaseHTTPRequestHandler, HTTPServer
import re
class MyHandler(BaseHTTPRequestHandler):
def _set_response(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_GET(self):
self._set_response()
self.wfile.write(bytes("""
<form action="/" method="post">
Val1:<br>
<input type="text" name="val1" value="1">
<br>
Val2:<br>
<input type="text" name="val2" value="2">
<br><br>
<input type="submit" value="Submit">
</form>
""", "utf-8"))
def do_POST(self):
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length)
post_data = re.split("&", post_data.decode("utf-8"))
val1 = int(re.split("=", post_data[0])[1])
val2 = int(re.split("=", post_data[1])[1])
self._set_response()
self.wfile.write(bytes("%s" % (val1 + val2), "utf-8"))
def run(server_class=HTTPServer, handler_class=MyHandler, port=8080):
server_address = ('', port)
httpd = server_class(server_address, handler_class)
print('Starting httpd...')
httpd.serve_forever()
if __name__ == "__main__":
run()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question