Sanitising user input using Python

What is the best way to sanitize user input for a Python-based web application? Is there a single function to remove HTML characters and any other necessary characters combinations to prevent an XSS or SQL injection attack? Here is a snippet that will remove all tags not on the white list, and all tag attributes not on the attribues whitelist (so you can't use onclick ). It is a modified

使用Python清理用户输入

清理基于Python的Web应用程序的用户输入的最佳方式是什么? 是否有单个函数来删除HTML字符和任何其他必要的字符组合以防止XSS或SQL注入攻击? 这里是一个代码片段,它将删除所有不在白名单上的标签,以及所有不属于白名单的标签属性(所以你不能使用onclick )。 它是http://www.djangosnippets.org/snippets/205/的一个修改版本,使用属性值的正则表达式来防止人们使用href="javascript:..." ,以及http: //ha.

How to pass a hidden recaptcha with mechanize?

I am trying to complete a form on a website automatically for academic purposes using Python's mechanize. When a human completes the form and submits it, there is no recaptcha. But when I fill in the controls for the form via mechanize in Python, there is a hidden control that is a recaptcha apparently. <HiddenControl(recaptcha_response_field=manual_challenge)> Since this recaptch

如何通过机械化隐藏的recaptcha?

我正在尝试使用Python的机械化自动完成学术目的的网站表单。 当一个人完成表格并提交时,没有任何回复。 但是当我通过Python中的机械化来填充表单的控件时,显然有一个隐藏的控件是一个recaptcha。 <HiddenControl(recaptcha_response_field=manual_challenge)> 由于这种回忆从来没有向人类展示,我不知道它在寻找什么,或者说这是一个手工挑战。 因此,我的问题是,我怎样才能通过这个挑战,让我可以继续自动化/

Silent printing of a PDF in Python

I'm trying to print a PDF with Python, without opening the PDF viewer application (Adobe, Foxit etc.). I need also to know when printing has finished (to delete the file). Here I found this implementation : import win32ui, dde, os.path, time from win32api import FindExecutable from os import spawnl, P_NOWAIT ... pd = "C:\temp\test.pdf" pdbits = os.path.split(pd) readerexe = FindExecutable

用Python静默打印PDF

我试图用Python打印PDF,而不用打开PDF查看器应用程序(Adobe,Foxit等)。 我还需要知道何时完成打印(删除文件)。 在这里我找到了这个实现 : import win32ui, dde, os.path, time from win32api import FindExecutable from os import spawnl, P_NOWAIT ... pd = "C:\temp\test.pdf" pdbits = os.path.split(pd) readerexe = FindExecutable(pdbits[1],pdbits[0]) spawnl(P_NOWAIT,readerexe[1],"DUMMY") #I added "DUMM

How to disable a Combobox in Tkinter?

Basically, I want to disable a certain Combobox, based on the value of another combobox. I couldn't find a answer to this question, maybe because it's very uncommon to do this to a Combobox. I have a code more or less as follow... self.cBox1Var=tki.StringVar() self.cBox1=ttk.Combobox(self.mframe, width=16, textvariable=self.cBox1Var, state='readonly',values=['Text entry','Comb

如何禁用Tkinter中的Combobox?

基本上,我想根据另一个组合框的值禁用某个组合框。 我无法找到这个问题的答案,也许是因为这对Combobox来说是非常罕见的。 我有一个或多或少的代码如下... self.cBox1Var=tki.StringVar() self.cBox1=ttk.Combobox(self.mframe, width=16, textvariable=self.cBox1Var, state='readonly',values=['Text entry','Combo box','Check button']) self.cBox1.grid(row=0,column=1,sticky=tki.W) self.cBox1Var.se

Convert floating point number to certain precision, then copy to String

I have a floating point number, say 135.12345678910 . I want to concatenate that value to a string, but only want 135.123456789 . With print, I can easily do this by doing something like: print "%.9f" % numvar with numvar being my original number. Is there an easy way to do this? With python < 3 (eg 2.6 [see comments] or 2.7), there are two ways to do so. # Option one older_method_str

将浮点数转换为某个精度,然后复制到字符串

我有一个浮点数,比如135.12345678910 。 我想将该值连接到一个字符串,但只想要135.123456789 。 通过打印,我可以通过执行以下操作轻松完成此操作: print "%.9f" % numvar numvar是我的原始数字。 是否有捷径可寻? 使用python <3(例如2.6 [见注释]或2.7),有两种方法可以这样做。 # Option one older_method_string = "%.9f" % numvar # Option two newer_method_string = "{:.9f}".format(numvar) 但是请

LDA gensim implementation, distance between two different docs

EDIT: I've found an interesting issue here. This link shows that gensim uses randomness in both training and inference steps. So what it suggested here is to set a fixed seed in order to get same results every time. Why however am I getting for every topic the same probability? What I want to do is to find for every twitter user her topics and calculate the similarity between twitter use

LDA gensim实现,两个不同文档之间的距离

编辑:我发现这里有一个有趣的问题。 该链接显示gensim在训练和推理步骤中都使用随机性。 所以它在这里建议的是设置一个固定的种子,以便每次获得相同的结果。 为什么我要为每个主题获取相同的概率? 我想要做的是为每个推特用户找到她的主题,并根据主题的相似性计算推特用户之间的相似度。 是否有可能为gensim中的每个用户计算相同的主题,或者是否必须计算主题词典并聚集每个用户主题? 总的来说,在gensim中基于主题

SQLAlchemy session error: InvalidRequestError

I am trying to commit as well as query the results at the same time from DB and i ended up with this error. sqlalchemy.exc.InvalidRequestError: This session is in 'committed' state; no further SQL can be emitted within this transaction. Full Traceback: Traceback (most recent call last): File "C:Python34libsite-packagesflaskapp.py", line 1836, in __call__ return self.wsgi_app(environ, sta

SQLAlchemy会话错误:InvalidRequestError

我试图提交以及从数据库同时查询结果,我结束了这个错误。 sqlalchemy.exc.InvalidRequestError: This session is in 'committed' state; no further SQL can be emitted within this transaction. 完全追溯: Traceback (most recent call last): File "C:Python34libsite-packagesflaskapp.py", line 1836, in __call__ return self.wsgi_app(environ, start_response) File "C:Python34libsite-packagesflaskapp.py

WTForm: FieldList with SelectField, how do I render?

I have this order form which allows my users to create an order. An order consists of multiple tuples of (producetype, quantity) . Producetype should be rendered in a <select> form while quantity can just be an input. The choices of producetype should be dynamically added because that could change. Currently, I've written this in bare html I would like to use WTForm for this becau

WTForm:带SelectField的FieldList,我该如何渲染?

我有这个订单,允许我的用户创建订单。 一个订单由(producetype, quantity)的多个元组组成。 Producetype应该以<select>形式呈现,而数量只能是输入。 农产品类型的选择应该动态添加,因为这可能会改变。 目前,我已经写在这个纯粹的HTML 我想为此使用WTForm,因为WTForm确实简化了我的代码。 但是,我无法这样做: 码: class OrderEntryForm(Form): quantity = IntegerField('Quantity',

How can I detect a palindrome in Hebrew?

I am writing a series of tests for a palindrome solver. I came across the interesting palindrome in Hebrew: טעם לפת תפל מעט Which is a palindrome, but the letter Mem has both a regular form (מ) and a "final form" (ם), how it appears as the last letter in a word. But short of hardcoding that "0x5de => 0x5dd" in my program, I was not able to figure out a way to programm

我如何检测希伯来语的回文?

我正在为回文求解器编写一系列测试。 我遇到了希伯来语有趣的回文: טעםלפתתפלמעט 这是一个回文,但Mem这个字母既有常规形式也有“最终形式”(ם),它看起来像是一个单词中的最后一个字母。 但是在我的程序中缺少硬编码,即“0x5de => 0x5dd”,我无法找到一种通过编程方式依靠Unicode,Python或将这两者视为相同的库的方法。 我做过的事情: s = 'טעם לפת תפל מעט' s.casefold() # Python 3.4 s.lower() s.upper() impor

urllib for python 3

This code in python3 is problematic: import urllib.request fhand=urllib.request.urlopen('http://www.py4inf.com/code/romeo.txt') print(fhand.read()) Its output is: b'But soft what light through yonder window breaks' b'It is the east and Juliet is the sun' b'Arise fair sun and kill the envious moon' b'Who is already sick and pale with grief' Why did I get b'...' ? What could I do to g

urllib for python 3

python3中的这个代码有问题: import urllib.request fhand=urllib.request.urlopen('http://www.py4inf.com/code/romeo.txt') print(fhand.read()) 它的输出是: b'But soft what light through yonder window breaks' b'It is the east and Juliet is the sun' b'Arise fair sun and kill the envious moon' b'Who is already sick and pale with grief' 为什么我得到b'...' ?我能做些什么才能得到正确的答案?