简体   繁体   English

使用web.py表单显示PostgreSQL数据库中的密码

[英]Display password from PostgreSQL database using web.py form

I am playing around with PostgreSQL and web.py and I have noticed that if I have a username and password in a database and given that the password may contain special characters that are members of string.printable , then when I want to print the queried password to the browser through a web.py template, something goes wrong with the character escaping and the password doesn't want to display. 我正在使用PostgreSQL和web.py,我注意到如果我在数据库中string.printable户名和密码,并且考虑到密码可能包含string.printable成员的特殊字符,那么当我要打印查询的内容时通过web.py模板向浏览器输入密码时,字符转义出了点问题,并且不想显示密码。 Instead the browser offers to download a text file (with no file extension) containing the password. 相反,浏览器提供下载包含密码的文本文件(无文件扩展名)的功能。

In my Python file: 在我的Python文件中:

class login:
    ...
    def POST(self):
        ...
        cursor.execute("SELECT password FROM tbl WHERE username = %s", (f['username'].value, ))
        realpassword = cursor.fetchone()
        realpassword = realpassword[0]
        ...
        return realpassword

The password appears correctly in the text file that downloads, but how do I display the password as text on the webpage? 密码正确显示在下载的文本文件中,但是如何在网页上将密码显示为文本?

Python string.printable includes both vertical-tab \\x0b and form-feed \\x0c , neither of which are friends to browsers. Python string.printable包括vertical-tab \\x0b和form-feed \\x0c ,这两个都不是浏览器的朋友。 Browsers assume they're receiving a file and offer to download it. 浏览器认为他们正在接收文件并愿意下载它。

(string.printable isn't the same as ASCII.) (string.printable与ASCII不同。)

Instead of returning the raw realpassword , return repr(realpassword) . 代替返回原始的realpassword ,返回repr(realpassword) Built-in repr() returns a "string containing a printable representation...", escaping control characters. 内置的repr()返回一个“包含可打印表示形式的字符串...”,转义控制字符。

>>> import string
>>> print string.printable[-20:]
=>?@[\]^_`{|}~  



>>> print repr(string.printable[-20:])
'=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM