Merge branch 'main' of https://gogs.gtsplab.com:5432/SVIG/python-bootcamp
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
# 5. What does this do? print("\007") Can you figure out why?
|
||||
|
||||
# It is a ASCII Bell character.
|
||||
# It generates a beep sound or a system alert depending on the platform and environment where the code is executed.
|
||||
# It is often used to trigger attention or alert the user in certain situations or as a way to generate a simple sound signal in text-based interfaces.
|
||||
|
||||
# BUT NOTHING HAPPENED ON MAC.
|
||||
|
||||
|
||||
|
||||
# 6. What does this do? print("abcdefg\rhijklmnop") Can you figure out why?
|
||||
|
||||
# '\r' is a carriage return character
|
||||
# When the string is printed, the carriage return character '\r' causes the cursor to return to the beginning of the line.
|
||||
# So when "hijklmnop" is printed, the cursor moves back to the start of the line, and the subsequent text "abcdefg" overwrites the initial characters.
|
||||
|
||||
# The output is gonna be 'hijklmnop'.
|
||||
|
||||
|
||||
|
||||
# 7. To shut down the HTTP server afterward, using Ctrl + C to interrupt the execution of the Python program.
|
||||
|
||||
import http.server
|
||||
import socketserver
|
||||
from urllib.parse import urlparse, parse_qs
|
||||
|
||||
class MyRequestHandler(http.server.SimpleHTTPRequestHandler):
|
||||
def do_GET(self):
|
||||
self.send_response(200)
|
||||
self.send_header('Content-type', 'text/plain')
|
||||
self.end_headers()
|
||||
output_string = "abcdefg\rhijklmnop"
|
||||
self.wfile.write(output_string.encode())
|
||||
|
||||
PORT = 8000
|
||||
Handler = MyRequestHandler
|
||||
httpd = socketserver.TCPServer(("", PORT), Handler)
|
||||
print(f"Serving on port {PORT}")
|
||||
|
||||
httpd.serve_forever()
|
||||
|
||||
# The HTTP server sends the response over the network to the client, which is typically a web browser.
|
||||
# In this function, the server sends an HTTP response with a status code of 200 and a content type of "text/plain".
|
||||
# The response body is set to the string "abcdefg\rhijklmnop" encoded as bytes.
|
||||
# When the client receives this response, it interprets the \r character as a normal character and displays the entire string "abcdefg\rhijklmnop" as it is.
|
||||
|
||||
# The '\r' character is a carriage return character, which is a control character used to return the cursor to the beginning of the current line.
|
||||
# The behavior of the carriage return character '\r' can depend on the platform and how the output is handled.
|
||||
|
||||
# For example:
|
||||
# In Safari and Chrome, the "http://localhost:8000" shows:
|
||||
# abcdefg
|
||||
# hijklmnop
|
||||
# In Macbook Terminal, the "curl http://localhost:8000" shows:
|
||||
# hijklmnop%
|
||||
|
||||
|
||||
|
||||
# 8. Think of a way someone might try to commit a crime or hide an embezzlement scheme using the answers to the above two questions.
|
||||
|
||||
# The key is by manipulating or altering the output displayed on a web page versus what is actually happening in the system.
|
||||
|
||||
# Case 1:
|
||||
# Show the false financial transactions to earn the cut.
|
||||
# Case 2:
|
||||
# Delivey the heist planning details and hide any suspicious activity.
|
||||
# Case 3:
|
||||
# Overwrite the previously displayed information for the false testimony purpose.
|
||||
|
||||
|
||||
|
||||
# 9. Think of how you would do input validation to prevent such a crime. Replace the code REPLACE ME here so the tests pass:
|
||||
|
||||
def validate(input_str):
|
||||
if '\r' in input_str or '\n' in input_str:
|
||||
raise ValueError("Crime detected!")
|
||||
|
||||
try:
|
||||
validate("abcdefg")
|
||||
print("✅ test1 pass!")
|
||||
except ValueError:
|
||||
print("❌ test1 fail!")
|
||||
|
||||
try:
|
||||
validate("abcdefg\rhijklmnop")
|
||||
print("❌ test2 fail!")
|
||||
except ValueError:
|
||||
print("✅ test2 pass!")
|
||||
|
||||
# Test 1 pass means there is no ValueError
|
||||
# Test 2 pass means the ValueError has been detected
|
||||
Reference in New Issue
Block a user