将字符串从 html 文件中的表单传递到尊重 utf-8 编码的 python 脚本

发布于 2024-10-19 23:26:17 字数 974 浏览 2 评论 0原文

我在index.html 文件中有此表单。

<form method="post" action="index.php" accept-charset="UTF-8">
    <input id="a" name="a" type="text">
    <input type="submit" name="run_query" value="Add User" size="30">
</form>

我试图通过将以下 php 代码嵌入到 index.html 文件中,将文本输入作为参数传递给 pyton 脚本:

<?
    session_start();
    ob_start();
    if(isset($_REQUEST['run_query'])) {
    $add_user = $_REQUEST['a'];
    $command = "add_author.py $add_user";
        exec($command); 
    }
?>

我已将 add_author.py 文件放入 index.html 所在的同一文件夹中。它适用于任何字符串。但如果我尝试使用包含 ä ö é 的字符串,它就不起作用。

python 文件看起来像这样。

import sys
import codecs
if __name__ == '__main__':
    wFile = codecs.open("test.txt", "w", "utf8")
    wFile.write(" ".join(sys.argv[1:]))
    wFile.close()

顺便说一下:index.html 中有这一行。

<meta charset="utf-8" />

我很想听到更好的方法来管理我的任务或纠正我的方法。谢谢你!

I have this form in a index.html file.

<form method="post" action="index.php" accept-charset="UTF-8">
    <input id="a" name="a" type="text">
    <input type="submit" name="run_query" value="Add User" size="30">
</form>

And I am trying to pass the text input to a pyton script as argument by embedding the following php code into the index.html file:

<?
    session_start();
    ob_start();
    if(isset($_REQUEST['run_query'])) {
    $add_user = $_REQUEST['a'];
    $command = "add_author.py $add_user";
        exec($command); 
    }
?>

I have put the add_author.py file into the same folder where index.html lays. It works fine with any string. But if I try to use strings which contains ä ö é it does not work.

The python file looks like this.

import sys
import codecs
if __name__ == '__main__':
    wFile = codecs.open("test.txt", "w", "utf8")
    wFile.write(" ".join(sys.argv[1:]))
    wFile.close()

By the way: The index.html has this line in it.

<meta charset="utf-8" />

I would love to hear from a better approach of managing my task or a correction of my approach. Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

就是爱搞怪 2024-10-26 23:26:17

为什么不让 PHP 写入文件而不是调用另一个 python 脚本?

if (!$handle = fopen("test.txt", 'a')) {
    echo "Cannot open file ($filename)";
    exit;
}

if (fwrite($handle, $_REQUEST['a']) === FALSE) {
    echo "Cannot write to file ($filename)";
    exit;
}

如果您坚持使用 python 脚本,也许您需要先对其进行编码,但通常会检查此处所述的不同方法:
http://docs.python.org/howto/unicode.html
我的猜测是你只需要在字符串上调用 unicode()

wFile.write(unicode(sys.argv[1:]))

Why not have PHP write to the file instead of calling another python script?

if (!$handle = fopen("test.txt", 'a')) {
    echo "Cannot open file ($filename)";
    exit;
}

if (fwrite($handle, $_REQUEST['a']) === FALSE) {
    echo "Cannot write to file ($filename)";
    exit;
}

If you insist on using the python script maybe you need to encode it first, but generally check with the different approaches stated here:
http://docs.python.org/howto/unicode.html
My guess is that you just need to call unicode() on the string

wFile.write(unicode(sys.argv[1:]))
最偏执的依靠 2024-10-26 23:26:17

您可以直接将 Python 与 CGI 结合使用。它应该比从 PHP 调用 Python 更快。配置也应该更容易。

简单的例子。

#!/usr/bin/python

import cgi;
import codecs;

form = cgi.FieldStorage()
my_a = form.getvalue("a","")

wFile = codecs.open("test.txt", "w", "utf8")
wFile.write(my_a);
wFile.close()

print("Content-Type: text/plain")
print("Location: ../plain.html")
print()

您必须将此 python 文件放入 CGI 脚本目录中。最常见的是/cgi-bin/。好吧,服务器可能也需要一些配置。

最后 3 行是简单的 http 标头。在我的示例中,它只是重定向到其他站点。没有可显示的内容。 getvalue("a","") 将返回字段“a”的值或空字符串(第二个参数)。嗯,几乎是常规的 Python 文件。

You can use Python directly with CGI. It should be faster than calling Python from PHP. It should be easier to configure too.

Simple example.

#!/usr/bin/python

import cgi;
import codecs;

form = cgi.FieldStorage()
my_a = form.getvalue("a","")

wFile = codecs.open("test.txt", "w", "utf8")
wFile.write(my_a);
wFile.close()

print("Content-Type: text/plain")
print("Location: ../plain.html")
print()

You have to put this python file into directory for CGI scripts. The most common is /cgi-bin/. Well, the server may need some configuration too.

The 3 last lines are simple http headers. In my example it just redirect to other site. There is no content to display. The getvalue("a","") will return value of field "a" or the empty string (second argument). Well, almost regular Python file.

王权女流氓 2024-10-26 23:26:17
# -*- coding: utf-8 -*-

文件顶部应强制使用 utf 编码。

# -*- coding: utf-8 -*-
import sys

if __name__ in '__main__':
    with open('test.txt','w') as out:
        out.write(''.join(sys.argv[1:]).encode("utf-8"))

应该可以正常工作

# -*- coding: utf-8 -*-

At the top of your file should force utf encoding.

# -*- coding: utf-8 -*-
import sys

if __name__ in '__main__':
    with open('test.txt','w') as out:
        out.write(''.join(sys.argv[1:]).encode("utf-8"))

Should work just fine

睫毛上残留的泪 2024-10-26 23:26:17

PHP 代码的实际问题似乎是添加到“命令”的参数“$add_user”没有以任何方式转义或保护。
这使得可以将任何内容发送到“exec”中,从而使系统容易受到攻击。网络漫画 XKCD 有一个关于此问题的“有趣”示例: http://xkcd.com/327/

您看到的原因是 UTF-8 编码的“åäö”以不可打印的字节开头,这会导致许多旧 shell 出现问题,具体取决于系统配置。

The actual problem with the PHP code seems to be that the argument "$add_user" added to the "command" is not escaped or secured in any way.
This makes it possible to send anything into the "exec", making the system vulnerable to attacks. The webcomic XKCD has a "funny" example of this problem: http://xkcd.com/327/

The cause of what you see is that UTF-8 encoded "åäö" begins with an unprintable byte that causes problems in many older shells, depending on system configuration.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文