鏈枃閾炬帴锛歨ttps://blog.csdn.net/qq_43467898/article/details/83187698
Django寰俊灏忕▼搴忓悗鍙板紑鍙戞暀绋
1 鐢宠灏忕▼搴忥紝鍒涘缓hello world灏忕▼搴
2 娣诲姞浜や簰妗嗗拰鎸夐挳
3 鍦ㄦ湇鍔″櫒閰嶇疆hello django
4 瀹炵幇璁$畻鍣ㄦ帴鍙
5 閰嶇疆鏈嶅姟鍣ㄥ皢鍚庣涓庡井淇″皬绋嬪簭杩炴帴
5.1 uwsgi閰嶇疆
5.2 http鍗忚锛80绔彛锛変笅鐨刵ginx閰嶇疆
5.3 https鍗忚锛443绔彛锛変笅鐨刵ginx閰嶇疆
5.4 閰嶇疆寰俊灏忕▼搴忕殑鏈嶅姟鍣ㄤ俊鎭
1 鐢宠灏忕▼搴忥紝鍒涘缓hello world灏忕▼搴
鍦ㄥ井淇″紑鍙戝钩鍙帮紙https://mp.weixin.qq.com锛夌敵璇峰皬绋嬪簭骞惰幏鍙朅PP id
涓嬭浇寰俊寮鍙戣呭伐鍏凤紙https://developers.weixin.qq.com/miniprogram/dev/devtools/download.html锛夛紝鎵撳紑鍚庣櫥褰曞苟濉叆APP id 绛変俊鎭
2 娣诲姞浜や簰妗嗗拰鎸夐挳
index. wxml
{{ result }}
index.wxss
/**index.wxss**/ .input { border: 1px solid black; margin-bottom: 5px; }
index.js
//index.js //鑾峰彇搴旂敤瀹炰緥 const app = getApp() Page({ data: { result: "鏆傛棤缁撴灉", formula: '' }, //浜嬩欢澶勭悊鍑芥暟 calculate: function () { wx.request({ url: 'https://shatter.xin/calculate', data: { formula: this.data.formula }, success: res => { if (res.statusCode == 200) { this.setData({ result: res.data }) } } }) }, input: function (e) { this.setData({ formula: e.detail.value }) } })
3 鍦ㄦ湇鍔″櫒閰嶇疆hello django
鍦ㄦ湇鍔″櫒瀹夎python3鍜宲ip3鐜锛屽苟瀹夎django
pip3 install django
鍒涘缓django椤圭洰
django-admin startproject calculator
cd calculator
淇敼calculator/settings.py涓殑ALLOWED_HOSTS = []涓篈LLOWED_HOSTS = ['*']
杩愯hello django椤圭洰
cd calculator
python3 manage.py runserver 0.0.0.0:8000
璁块棶http://鏈嶅姟鍣╥p:8000鍙互鐪嬪埌涓嬪浘锛
4 瀹炵幇璁$畻鍣ㄦ帴鍙
鍒涘缓django app
python3 manage.py startapp CalculateApi
鍦╟alculator/settings.py鐨処NSTALLED_APPS涓坊鍔燙alculateApi濡備笅锛
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'CalculateApi'
]
鍦╟alculator/urls.py涓皢url杞彂缁機alculateApi澶勭悊銆
from django.contrib import admin
from django.urls import path
from django.conf.urls import url, include
urlpatterns = [
path('admin/', admin.site.urls),
url('^', include('CalculateApi.urls')),
]
鍦–alculateApi涓柊寤簎rls.py鏂囦欢锛屽鐞/calculate鎺ュ彛銆
from django.conf.urls import url
from . import views
urlpatterns = [
url('calculate', views.calculate)
]
鍦–alculateApi/views.py鏂囦欢涓坊鍔燾alculate鍑芥暟鐢ㄤ簬璁$畻姹傚煎苟杩斿洖銆
from django.http import HttpResponse
def calculate(request):
formula = request.GET['formula']
try:
result = eval(formula, {})
except:
result = 'Error formula'
return HttpResponse(result)
鍐嶆杩愯鏈嶅姟鍣紝璁块棶http://鏈嶅姟鍣╥p:8000/calculate?formula=2*3-5鍗冲彲寰楀埌缁撴灉1銆
5 閰嶇疆鏈嶅姟鍣ㄥ皢鍚庣涓庡井淇″皬绋嬪簭杩炴帴
鐢变簬寰俊瑕佹眰浣跨敤https鍗忚杩涜閫氳锛屾垜浠娇鐢╪ginx + uwsgi + django鏉ラ厤缃甴ttps鏈嶅姟鍣ㄣ
5.1 uwsgi閰嶇疆
瀹夎uwsgi
pip3 install uwsgi
閰嶇疆django椤圭洰鐨剈wsgi.ini锛屽湪calculator鏂囦欢澶逛腑鏂板缓uwsgi.ini鏂囦欢
touch uwsgi.ini
vi uwsgi.ini
杈撳叆浠ヤ笅閰嶇疆
[uwsgi]
# django椤圭洰鐩戝惉鐨剆ocket鏂囦欢锛堝彲浠ヤ娇鐢ㄧ鍙d唬鏇匡級
socket = ./calculator.sock
# django椤圭洰鎵鍦ㄧ洰褰
chdir = .
# django椤圭洰wsgi鏂囦欢
wsgi-file = ./calculator/wsgi.py
master = true
processes = 2
threads = 4
vacuum = true
# 閫氳繃touch reload鍙互閲嶅惎uwsgi鏈嶅姟鍣
touch-reload = ./reload
# 鏃ュ織杈撳嚭
daemonize = calculator.log
杩愯uwsgi鏈嶅姟鍣
uwsgi --ini uwsgi.ini
touch reload
5.2 http鍗忚锛80绔彛锛変笅鐨刵ginx閰嶇疆
瀹夎nginx
sudo apt-get install nginx
cd /etc/nginx
淇敼nginx鐢ㄦ埛
vi nginx.conf
灏嗙涓琛屼慨鏀逛负
user root;
娣诲姞80绔彛鐨勯厤缃枃浠
cd conf.d
sudo touch calculator.conf
sudo vi calculator.conf
濉叆浠ヤ笅閰嶇疆锛
server{
listen 80;
server_name 鏈嶅姟鍣╥p;
charset UTF-8;
client_max_body_size 75M;
location ~ ^/calculate {
// replace "path" to the path of your project
uwsgi_pass unix:///"path"/calculator/calculator.sock;
include /etc/nginx/uwsgi_params;
}
}
閲嶅惎nginx鏈嶅姟鍣
sudo service nginx restart
璁块棶鏈嶅姟鍣ㄧ殑80绔彛鍗冲彲璁块棶calculate鎺ュ彛锛屽http://鏈嶅姟鍣╥p/calculate?formula=2*3-4
5.3 https鍗忚锛443绔彛锛変笅鐨刵ginx閰嶇疆
濡傛灉鏈夎嚜宸辩殑鍩熷悕鍜宻sl璇佷功锛屽皢calculator.conf閰嶇疆鏂囦欢淇敼濡備笅锛
server{
listen 443;
server_name your.domain;
ssl on;
ssl_certificate path/to/your/ssl.pem;
ssl_certificate_key path/to/your/ssl.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
charset UTF-8;
client_max_body_size 75M;
location ~ ^/calculate {
uwsgi_pass unix:///path/to/calculator/calculator.sock;
include /etc/nginx/uwsgi_params;
}
}
閲嶅惎nginx鏈嶅姟鍣紝璁块棶鏈嶅姟鍣ㄧ殑443绔彛鍗冲彲璁块棶calculate鎺ュ彛锛屽https://鏈嶅姟鍣ㄥ煙鍚/calculate?formula=2*3-4
濡傛灉浣犲彧鏈夎嚜宸辩殑鍩熷悕鑰屾病鏈塻sl璇佷功锛屽彲浠ュ幓鐢宠鍏嶈垂鐨剆sl璇佷功鎴栬呭弬鑰冩缃戝潃閰嶇疆锛坔ttps://certbot.eff.org/#ubuntuxenial-nginx锛夈
濡傛灉浣犳病鏈夎嚜宸辩殑鍩熷悕鐢氳嚦娌℃湁鑷繁鐨勬湇鍔″櫒锛岃鍑洪棬鍙宠浆闃块噷浜戞垨宸﹁浆鑵捐浜戣嚜琛岃喘涔般
5.4 閰嶇疆寰俊灏忕▼搴忕殑鏈嶅姟鍣ㄤ俊鎭
杩愯灏忕▼搴忥紝涓涓畝鍗曠殑璁$畻鍣ㄥ氨鍐欏畬鍟︺
浠ヤ笂灏辨槸Django寰俊灏忕▼搴忓悗鍙板紑鍙戞暀绋嬬殑璇︾粏鍐呭锛屾洿澶氫俊鎭鍏虫敞OD浜戝叾瀹冪浉鍏虫枃绔狅紒