socket简易后台
import socket
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(('127.0.0.1', 8801))
print('浏览器访问:http://127.0.0.1:8801')
server.listen(5)
conn, _ = server.accept()
data = conn.recv(1024)
print(data)
# 响应一定要满足http协议
conn.send(b'HTTP1.1 200 OK\r\n\r\nhehe\r\n')
flask后台
# pip3 install flask
# pip3 install Flask-Cors
from flask import Flask, request
from flask_cors import CORS
import json
# 声明服务
server = Flask(__name__)
# 解决ajax跨域无法拿到数据
CORS(server, supports_credentials=True)
# 处理请求与响应的函数
@server.route('/')
def home():
return '<h1>Home Page</h1>'
@server.route('/login')
def login():
return '<h1>Login Page</h1>'
@server.route('/data')
def data():
print(request.args)
print(request.args.get('username'))
print(request.args.get('password'))
res_data = {
'status': 0,
'msg': 'request success',
'data': {
'name': 'Owen',
'age': 18
}
}
return json.dumps(res_data)
# 启动服务器
if __name__ == '__main__':
server.run(host='localhost', port=8801)