PHP 网页 "translated" 到 Python 网络应用
PHP web page "translated" to Python web app
我想知道这个(我的 'index.php')单页下载网站(我制作的)是否可以 "translated" 到 python 网络应用程序('get.py').
这是来自 'index.php' 的 PHP:
<?php
echo '
<form method="post">
<input type="text" class="text" placeholder="URL" name="url">
<input type="submit" class="submit" name="Submit" value="Get Link">
</form>
';
if (isset($_POST['Submit'])) {
$url = $_POST['url'];
echo '<br><a href="http://'.$url.'" download>Download</a><br>';
}
?>
谢谢!!
我建议使用 Javascript 而不是 Python 来处理这个问题。 Python 是一种纯后端语言,而 Javascript 的语法与 PHP 几乎相同。如果您尝试使用 Python 执行此操作,您还必须使用 Javascript 来处理前端。
读这个我 5 年前问的问题很有趣,但我现在来这里是为了回答我自己的问题,以供可能像我一样学习 PHP 并想解决“转换” 像这样的基本东西到更“现代”的方法。
在问这个问题时,我对网络开发还很陌生,不知道自己想要什么或需要什么。我只被告知 Python 比 PHP 更可取,尽管他们的 意见 ,而且我对这个主题的任何批判性思考都太新鲜了。
与 PHP 一样,Python 是一种后端语言(这意味着它在服务器上执行,而不是在浏览器上执行)。所以从技术上讲,您可以使用 python 框架(例如 Django or Flask)来实现这一点。
但是,正如 bpow 所建议的那样:
I would recommend using Javascript to handle this instead of Python. Python is a purely backend language, whereas Javascript has nearly identical syntax as PHP. If you were to try to do this with Python, you'd also have to use Javascript to handle the frontend.
JavaScript 更适合这种情况,因为我们完全可以在浏览器中完成的事情实际上不需要联系服务器。
我的建议是选择一个 JavaScript 框架并使用它。我会推荐 VueJS 因为它是开源的并且非常容易上手,你只需要在 HTML <head>
中包含 CDN <script>
标签。下面是它在 VueJS 中的外观。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Download Link Generater</title>
<!-- development version, includes helpful console warnings -->
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>
<body>
<div id="app">
<input type="text" v-model='link_to_download' placeholder="Enter URL of asset you wish to download">
<a v-bind:href='link_to_download' download>Download {{ link_to_download }}</a>
</div>
<script>
var app = new Vue({
el: '#app',
data: {
link_to_download: '' // This is updated by the text input because it is bound by the 'v-model' attribute. When this changes, it also updates everywhere it is used in the DOM, so here being the href url and the text in the <a> hyperlink.
}
})
</script>
</body>
</html>
因为 Vue 是一个 反应式 框架,文本输入会在您键入时更新 data
挂钩中的 link_to_download
变量。随着 data
挂钩中的变量发生变化(通过 v-model
绑定),DOM 中的任何地方都使用该变量 对变化的变量做出反应 并对其进行镜像,从而无需后端服务器来“生成”下载 link.
我想知道这个(我的 'index.php')单页下载网站(我制作的)是否可以 "translated" 到 python 网络应用程序('get.py').
这是来自 'index.php' 的 PHP:
<?php
echo '
<form method="post">
<input type="text" class="text" placeholder="URL" name="url">
<input type="submit" class="submit" name="Submit" value="Get Link">
</form>
';
if (isset($_POST['Submit'])) {
$url = $_POST['url'];
echo '<br><a href="http://'.$url.'" download>Download</a><br>';
}
?>
谢谢!!
我建议使用 Javascript 而不是 Python 来处理这个问题。 Python 是一种纯后端语言,而 Javascript 的语法与 PHP 几乎相同。如果您尝试使用 Python 执行此操作,您还必须使用 Javascript 来处理前端。
读这个我 5 年前问的问题很有趣,但我现在来这里是为了回答我自己的问题,以供可能像我一样学习 PHP 并想解决“转换” 像这样的基本东西到更“现代”的方法。
在问这个问题时,我对网络开发还很陌生,不知道自己想要什么或需要什么。我只被告知 Python 比 PHP 更可取,尽管他们的 意见 ,而且我对这个主题的任何批判性思考都太新鲜了。
与 PHP 一样,Python 是一种后端语言(这意味着它在服务器上执行,而不是在浏览器上执行)。所以从技术上讲,您可以使用 python 框架(例如 Django or Flask)来实现这一点。
但是,正如 bpow 所建议的那样:
I would recommend using Javascript to handle this instead of Python. Python is a purely backend language, whereas Javascript has nearly identical syntax as PHP. If you were to try to do this with Python, you'd also have to use Javascript to handle the frontend.
JavaScript 更适合这种情况,因为我们完全可以在浏览器中完成的事情实际上不需要联系服务器。
我的建议是选择一个 JavaScript 框架并使用它。我会推荐 VueJS 因为它是开源的并且非常容易上手,你只需要在 HTML <head>
中包含 CDN <script>
标签。下面是它在 VueJS 中的外观。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Download Link Generater</title>
<!-- development version, includes helpful console warnings -->
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>
<body>
<div id="app">
<input type="text" v-model='link_to_download' placeholder="Enter URL of asset you wish to download">
<a v-bind:href='link_to_download' download>Download {{ link_to_download }}</a>
</div>
<script>
var app = new Vue({
el: '#app',
data: {
link_to_download: '' // This is updated by the text input because it is bound by the 'v-model' attribute. When this changes, it also updates everywhere it is used in the DOM, so here being the href url and the text in the <a> hyperlink.
}
})
</script>
</body>
</html>
因为 Vue 是一个 反应式 框架,文本输入会在您键入时更新 data
挂钩中的 link_to_download
变量。随着 data
挂钩中的变量发生变化(通过 v-model
绑定),DOM 中的任何地方都使用该变量 对变化的变量做出反应 并对其进行镜像,从而无需后端服务器来“生成”下载 link.