zoukankan      html  css  js  c++  java
  • Flask:实现从浏览器访问运行python程序--使用GET方法添加动态的参数03

    实现的功能如下

    1.运行自己写的python程序(.py),将此程序作为服务器监听

    2.从浏览器输入相应的ip,端口,这里还实现了输入相应的参数,可以传递参数并运行.py程序

    3.运行程序后,实现电子邮件加密后发送

     服务器端:

    # 编写人:Jaoany
    # 开发时间:2021/7/27 15:01
    from flask import Flask
    from flask import request

    # 发送邮件,使用RSA密码算法加密,需要接收方使用自己的私钥对密文解密
    # 导入第三方模块
    import smtplib
    from email.mime.text import MIMEText
    from email.utils import formataddr

    print("Content-type:text/html")

    # 在这里可以自己写函数

    app = Flask(__name__)


    @app.route('/', methods=['GET', 'POST'])
    def home():
    return '<h1>Home</h1>'

    #如果想动态传参数,
    # 在本地浏览器输入(在另外一台电脑浏览器的还没测试)
    http://127.0.0.1:5000/sender/邮件内容
    localhost:5000/sender/邮件内容
    #在"/sender"后面添加"/<plaintext1>",然后你就可以引用了

    @app.route("/sender/<plaintext1>", methods=['GET'])
    def sender(plaintext1):
    p, q, e = make_p_q_e()
    # 获取数据
    # plaintext1 = input("待发送加密数据:")
    # plaintext1 = '1234567'
    # 将输入的数据转换成01字符串
    plaintext = ' '.join(format(ord(x), 'b') for x in plaintext1)
    # 公钥、私钥
    public_key, private_key = make_key(p, q, e)
    # 加密
    ciphertext1 = encryption(public_key, plaintext)
    print("加密后的数据:", ciphertext1)
    # 将数组转换成字符串
    ciphertext = ', '.join(str(i) for i in ciphertext1)
    # 输出加密后的字符串数据
    print(ciphertext)
    # 调用函数(登录密码需要换成你自己的)
    mail('发件人邮箱@qq.com', '发件人邮箱SMTP验证码', '收件人邮箱.com', '发件人昵称', '收件人昵称', ciphertext)

    return '<h3>邮件发送成功! {}</h3>'.format(plaintext1)


    if __name__ == '__main__':
    app.run()

     更新:后来带我的领导说我写的好像参数没有传递,让我改一下,能够在浏览器查看参数路径的那种,如下,

     修改代码中主要是用了一个函数来接收参数:

     具体route中的代码如下:

    # 记住头部要添加:端口

    # 只接受get方法访问
    @app.route("/test_1.0", methods=["GET"])
    def check():
    p, q, e = make_p_q_e()
    # 获取数据
    # plaintext1 = input("待发送加密数据:")
    # plaintext1 = '1234567'
    # 获取传入的params参数
    get_data = request.args.to_dict()
    to_user = get_data.get('receiverMail')
    to_nick = get_data.get('receiverName')
    subject = get_data.get('subject')
    plaintext1 = get_data.get('message')
    # 将输入的数据转换成01字符串
    plaintext = ' '.join(format(ord(x), 'b') for x in plaintext1)
    # 公钥、私钥
    public_key, private_key = make_key(p, q, e)
    # 加密
    ciphertext1 = encryption(public_key, plaintext)
    print("加密后的数据:", ciphertext1)
    # 将数组转换成字符串
    ciphertext = ', '.join(str(i) for i in ciphertext1)
    # 输出加密后的字符串数据
    print(ciphertext)

    return_dict = {'return_info': '发送成功', 'message': False}
    # 判断入参是否为空
    if request.args is None:
    return_dict['return_code'] = '5004'
    return_dict['return_info'] = '请求参数为空'
    return json.dumps(return_dict, ensure_ascii=False)

    # 调用函数(登录密码需要换成你自己的)
    mail('3025620084@qq.com', 'usymoatskbjlddcf', to_user, '草璧月', to_nick, subject, ciphertext)

    # 对参数进行操作
    return_dict['message'] = tt(subject, plaintext1)

    return json.dumps(return_dict, ensure_ascii=False)


    # 功能函数
    def tt(subject, plaintext1):
    result_str = "邮件主题:%s,邮件内容:%s" % (subject, plaintext1)
    return result_str


    if __name__ == "__main__":
    app.run(debug=True)

    本文来自博客园,作者:Jaoany,转载请注明原文链接:https://www.cnblogs.com/fanglijiao/p/15070502.html

  • 相关阅读:
    poj2886 Who Gets the Most Candies?
    poj1201 Intervals
    poj3109 Inner Vertices
    poj1990 MooFest
    poj3368 Frequent values
    NOIP练习赛题目6
    NOIP练习赛题目5
    NOIP练习赛题目4
    NOIP练习赛题目3
    NOIP练习赛题目2
  • 原文地址:https://www.cnblogs.com/fanglijiao/p/15070502.html
Copyright © 2011-2022 走看看