1. 一个小的demo-来自廖雪峰的教程:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001432012745805707cb9f00a484d968c72dbb7cfc90b91000
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from flask import Flask
from flask import request
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST']) #http://127.0.0.1:5000/ go to main page
def home():
return '<h1>Home</h1>'
@app.route('/signin', methods=['GET']) #http://localhost:5000/signin go to sign in form.
def signin_form():
return '''<form action="/signin" method="post">
<p><input name="username"></p>
<p><input name="password" type="password"></p>
<p><button type="submit">Sign In</button></p>
</form>'''
@app.route('/signin', methods=['POST'])
def signin():
# 需要从request对象读取表单内容:
if request.form['username']=='admin' and request.form['password']=='password':
return '<h3>Hello, admin!</h3>'
return '<h3>Bad username or password.</h3>'
if __name__ == '__main__':
app.run()
# Run in a specify sever?
# app.run(
# host = '0.0.0.0',
# port = 7777,
# debug = True
# )
2. 清除占用了localhost的process:
kill the process currently using a port on localhost in windows
(1)Run command-line as an Administrator.
netstat -ano | findstr :yourPortNumber
(2)Then you execute this command after identify the PID.
(3) Done!!