Python
/
Strings
- 1 Language 9
-
Hello World
-
Variables
-
Functions
-
Conditional
-
Operators
-
While
-
Turtle
-
Script Mode
-
Debugging
- 2 Strings 6
-
Slice
-
Raw Strings
-
Regex
-
Validation
-
Config
-
Escape
- 3 Collections 5
-
Lists
-
Dictionaries
-
Efficiency
-
Tuples
-
References
- 4 Functions 5
-
Recursion
-
Factorial
-
Modulus
-
Reassignment
-
Approximate
- 5 Storage 8
-
Files
-
Databases
-
Pipes
-
With open
-
Shelve
-
Zip
-
Csv
-
Json
- 6 Class 4
-
Definition
-
Attributes
-
Functional
-
Methods
- 7 Goodies 5
-
Conditional Expression
-
List Comprehension
-
Generator
-
Named Tuple
-
Modules
- 8 Applications 5
-
Pythagora
-
Palindrome
-
Binary Search
-
Conway Game
-
Coin Flip
- 9 Scheduler 4
-
Time
-
Multithreading
-
Subprocess
-
Logging
- 10 Packages 2
-
Clipboard
-
Ocr
/
Escape
➟
➟
Last update: 12-05-2022
Escape
! Prevent xss attacks with html escape.
"""XSS
Prevent cross site scriting attacks.
Escape html tags with html library.
"""
import html
s1 = """& < " ' >"""
o1 = html.escape(s1)
print(o1)
# & < " ' >
s2 = "<script>alert('hack');</script>"
o2 = html.escape(s2)
print(o2)
# <script>alert('hack');</script>
assert o1 == '& < " ' >'
assert o2 == '<script>alert('hack');</script>'
print("pass")
XML
The sax library escape should execute faster.
"""XSS
Prevent cross site scriting attacks.
Escape xml tags.
"""
from xml.sax.saxutils import escape
from xml.sax.saxutils import quoteattr
s1 = '< & >'
o1 = escape(s1)
print(o1)
# < & >
s2 = "a ' b"
o2 = quoteattr(s2)
print(o2)
# "a ' b"
assert o1 == '< & >'
assert o2 == '"a \' b"'
print('pass')
➥ Questions github Strings