我们正在使用名为 FastAPI 的 Python 框架开发 REST API。html 注入代码安全测试失败。他们在 post 有效负载中发送一些 html 标签代码,我们将其插入到数据库中并在 GET 响应中发送相同的代码。在 FastAPI 中处理请求时,有什么方法可以防止这种 HTML 注入。
我有一个小程序,代码如下:
def get_code(hex_pattern, database='./AndroidLockScreenRainbow.sqlite'):
try:
if os.path.exists(database):
with lite.connect(database) as db:
with db.cursor() as c:
c.execute("SELECT * FROM RainbowTable")
rows = c.fetchall()
for row in rows:
if row[0] == hex_pattern:
return row[1]
else:
raise lite.OperationalError("Database file not exists")
except lite.OperationalError:
print('Given SQL table not found!')
Run Code Online (Sandbox Code Playgroud)
当代码到达 db.cursor() as c: 的行时,程序给出以下错误
Run Code Online (Sandbox Code Playgroud)with db.cursor() as c: AttributeError: __exit__
我有什么错吗?
如果用户已经通过身份验证,我想重定向到主页。所以我想从/重定向到/ main
views.py:
class LoginView(FormView):
form_class = LoginForm
success_url = reverse_lazy('main')
template_name = 'module/login.html'
def form_valid(self, form):
if self.request.user.is_authenticated():
return redirect(settings.LOGIN_REDIRECT_URL)
else:
username = form.cleaned_data['username']
password = form.cleaned_data['password']
user = authenticate(username=username, password=password)
if user is not None and user.is_active:
login(self.request, user)
return super(LoginView, self).form_valid(form)
else:
return self.form_invalid(form)
Run Code Online (Sandbox Code Playgroud)
settings.py:
LOGIN_REDIRECT_URL = reverse_lazy('main')
LOGIN_URL = reverse_lazy('login')
LOGOUT_URL = reverse_lazy('logout')
Run Code Online (Sandbox Code Playgroud)
我也尝试添加一个get函数,但是随后它只能无限次数地调用自身。
def get(self, request, *args, **kwargs):
if request.user.is_authenticated():
return HttpResponseRedirect('main')
else:
return HttpResponseRedirect('/login/')
Run Code Online (Sandbox Code Playgroud)
urls.py:
url(r'^$', views.LoginView.as_view(), name='login'),
url(r'^login/', views.LoginView.as_view(), name='login'),
Run Code Online (Sandbox Code Playgroud) 我创建了很多类的实例。然后我想通过名称找到一个实例。但我收到错误消息TypeError: get() missing 1 required positional argument: 'value'。
class Test(object):
def __init__(self, value):
self.value = value
def get(self, value):
if self.value == value:
return self
else:
return None
test_obj = Test('foobar')
print(test_obj.value)
instance = Test.get('foobar')
if instance:
print(instance.value)
Run Code Online (Sandbox Code Playgroud) 我想与来自我的 bash 脚本的变量一起运行 python 内联命令。它在没有导入行的情况下工作。
for filename in *.txt; do
OUT=$(python3 -c "import pprint\nprint('$filename')")
mv $file $OUT
done
Run Code Online (Sandbox Code Playgroud)
但我收到错误消息SyntaxError: unexpected character after line continuation character。