连接Python,MySQL和HTML [英] Connecting Python, MySQL and HTML

查看:611
本文介绍了连接Python,MySQL和HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我正在尝试做的事情的概述:我正在从GSM调制解调器读取数据,而 我将它存储在数据库中。

  • 我使用Python脚本和MySQL数据库执行此操作。


  • 所以这里Python只是一个后端,我想用一个HTML文件创建一个前端。这将在稍后在服务器上进行。 (我希望为HTML文件创建一个特殊的'外观')。

  • 我首先创建HTML文件,然后将其与Python链接?请注意,Python脚本使用实时数据填充数据库。



    或者我是否自己创建了这个HTML文件?我是否能够达到我想要的网页的感觉。



    另外我会在HTML页面中有一些选项。当选择其中一个选项时,Python脚本需要能够对其作出反应并在GSM调制解调器上发送消息。那么我可以用HTML来开始运行一个Python脚本吗?



    我该怎么做才能达到上述目的?

    解决方案

    你基本需要的是 MVC (模型视图控制器),其不同的组件将是:
    $ b


    1. Model:包含数据的MySQL数据库

    2. 查看:您的HTML前端

    3. 控制器:您的主逻辑所在的位置,例如点击UI时发送给调制解调器的命令,或从UI中的数据库和绘图中提取什么数据

    Web框架是要走的路。对于一个非常简单的项目(您想在不到一个小时内完成), web.py flask 瓶子 a>会很好。对于更详细的内容,您可能需要使用 Django



    我通常在前端之间进行通信的方式而后端正在使用AJAX查询。这种方法的优点是,明天如果您决定更改底层框架,您的UI代码将保持不变。



    现在让我们看一个示例任务:用户选择一个特定的命令发送到GSM调制解调器,并等待响应,如果它成功或没有。


    1. 在UI上,您可以发送包含有关该命令的信息的AJAX POST或GET请求。让我们假设它是类似AT的,所以你决定将它发布到你在路由信息中定义的URL,让它成为/执行

    2. 现在,相应的函数路由条目得到执行,它将检索参数AT,使调制解调器理解必要的数据包并发送它并从Modem获得一些响应。
    3. 函数RETURNS with this响应,然后控件返回到UI

    4. 在UI中,您可以使用代码来显示要执行的任务的状态

    您可以采用类似的方式从数据库中提取数据。


    Here's an overview of what I'm trying to do:

    • I'm reading data from a GSM modem and I am storing this in a database.

    • I'm doing this using a Python script and a MySQL database.

    • So here Python is just a back end, I want to create a front end using an HTML file. This would go up on a server later. (There is a particular 'look' I want to create for the HTML file.)

    So do I first create the HTML file and then link it with Python? Do note that the Python script populates the database with live data.

    Or do I create this HTML file in Python itself? Will I be able to achieve the feel I want for the webpage.

    Also I shall have some options in the HTML page. When one of these options are selected, the Python script needs to be able to react to it and send out a message on the GSM modem. So can I get HTML to start running a Python script?

    What do I do to achieve the above?

    解决方案

    What you basically need is MVC (Model View Controller), whose different components would be:

    1. Model: Your MySQL database containing the data
    2. View: Your HTML front end
    3. Controller : Where your main logic would reside, like what command to send to Modem when something on UI is clicked, or what data to pull from database and plot in the UI

    Web Frameworks are the way to go. For a very simple project (you wanna finish it in less than an hour), web.py, flask and bottle would be good. For something more elaborate you may want to go with Django.

    Each of these allow you to create your own HTML files and communicate with the backend using some templating scheme and control logic.

    The way i usually communicate between the frontend and the backend is using AJAX queries. The advantage of this approach is that tomorrow if you decide to change the underlying framework, your UI code remains the same.

    Let us now take one sample task: The user selects a particular command to send to the GSM Modem and waits for the response, if it were successful or not.

    1. On the UI you can send an AJAX POST or GET Request containing information about the command. Let us assume it is something like "AT", so you decide to POST it to a URL you have defined in your Routes information, let it be /execute
    2. Now the function corresponding to that Route entry gets executed, which will retrieve the parameter, being "AT", will make the necessary packet understood by the Modem and send it and get some response from Modem
    3. The function RETURNS with this response, and the control goes back to the UI
    4. In the UI you can have code to show the status of the task you wanted to execute

    You can do similarly for pulling data from Database.

    这篇关于连接Python,MySQL和HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆