【发布时间】:2020-11-15 04:48:34
【问题描述】:
我已经制作了电子邮件联系表,用于发送邮件。
前端是用 HTML/CSS 和 JS 制作的。 index.html后面的JS如下:
contactForm = document.getElementById("contact_form");
contactForm.onsubmit = (element) => {
element.preventDefault();
name = document.getElementById("form_name").value;
email = document.getElementById("form_email").value;
subject = document.getElementById("form_subject").value;
message = document.getElementById("form_message").value;
raw = JSON.stringify({
name: name,
email: email,
subject: subject,
message: message,
});
console.log(raw);
fetch("http://example.com:5000/contact", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: raw,
redirect: "follow",
})
.then((response) => response.text())
.then((result) => {
console.log(result);
if (result == "received") {
alert("Your message has been sent");
} else {
alert("Sorry we couldn't send your message, please try again");
}
})
.catch((error) => console.log("error", error));
};
烧瓶应用:
from flask import Flask, request
from pprint import pprint
from flask_cors import CORS
import smtplib
from email.message import EmailMessage
app = Flask(__name__)
CORS(app)
email_add = 'USER'
email_pass = 'PASS'
@app.route('/')
def verify():
return "app is running"
@app.route('/contact', methods=['POST'])
def post_email():
request_data = request.json
pprint(request_data)
if 'email' and 'message' and 'name' and 'subject' in request_data:
msg = EmailMessage()
msg['Subject'] = request_data['subject']
msg['From'] = request_data['email']
msg['To'] = email_add
name = request_data['name']
msg.set_content(" From " + name + ", " + "\n\n" + " " + request_data['message'] + "\n\n" +
"My email: " + request_data['email'])
pprint(request_data['subject'])
smtp = smtplib.SMTP_SSL('smtp.gmail.com', 465)
smtp.login(email_add, email_pass)
smtp.send_message(msg)
return "received"
else:
return "sorry"
if __name__ == '__main__':
app.run()
应用程序托管在 linux server 上,客户端文件使用 Nginx 提供,并且烧瓶 API 使用 uwsgi 提供 gunicorn。我在 systemd 中创建了一个服务文件,它负责在 0.0.0.0:5000 运行烧瓶应用程序。一切工作正常,但是当我从letsencrypt安装SSL证书时,客户端文件工作正常,但我的flask API不再工作了。它一直说无法获取,使用了混合协议。我应该如何解决这个问题。我试过 pyOpenSSL 但它不起作用。无论如何我可以在我的 API 和客户端服务器中使用 SSL。谢谢你。
这就是我为我的应用提供服务的方式。
server {
server_name www.example.com;
root /home/jimrahman/dark_second;
index index.html;
location /contact {
include proxy_params;
proxy_pass https://0.0.0.0:5000;
}
【问题讨论】:
标签: ssl nginx flask gunicorn uwsgi