Update page 'Day 2 homework - Sloetjes'
+16
-16
@@ -1,7 +1,7 @@
|
||||
# Xuanyi Wang\_Day-2\_Homework\_03/31/23
|
||||
|
||||
|
||||
#### 1. Annotate the following python expressions with the specific types of each literal, as well as the overall return value:
|
||||
##### 1. Annotate the following python expressions with the specific types of each literal, as well as the overall return value:
|
||||
> i. i = 3
|
||||
>
|
||||
> ii. f = 3.0
|
||||
@@ -24,7 +24,7 @@
|
||||
```
|
||||
The overall return value of each expression is the value assigned to the corresponding variable, which is the literal itself.
|
||||
|
||||
#### 2. For each of the types string, bytestring, integer, floating point, and imaginary literals, construct a 5x5 table showing what happens when the character "0" or number 0 is compared to each other type.
|
||||
##### 2. For each of the types string, bytestring, integer, floating point, and imaginary literals, construct a 5x5 table showing what happens when the character "0" or number 0 is compared to each other type.
|
||||
|
||||
| | **'0'** | **b'0'** | **0** | **0.0**| **0j** |
|
||||
|---|---------|---------|---------|---------|---------|
|
||||
@@ -34,9 +34,9 @@ The overall return value of each expression is the value assigned to the corresp
|
||||
| **0.0**| Not equal | Not equal | Equal | Equal | Not Equal |
|
||||
| **0j** | Not equal | Not equal | Equal | Not equal| Equal |
|
||||
|
||||
#### 3. Now do the same thing for the + and == operators for each pair of types in two other 5x5 tables.
|
||||
##### 3. Now do the same thing for the + and == operators for each pair of types in two other 5x5 tables.
|
||||
|
||||
#### The "+" operator case:
|
||||
##### The "+" operator case:
|
||||
| | **'0'** | **b'0'** | **0** | **0.0**| **0j** |
|
||||
|---|---------|---------|---------|---------|---------|
|
||||
| **'0'** | '00' | TypeError | TypeError | TypeError | TypeError |
|
||||
@@ -45,7 +45,7 @@ The overall return value of each expression is the value assigned to the corresp
|
||||
| **0.0**| TypeError | TypeError | 0.0 | 0.0 | TypeError |
|
||||
| **0j** | TypeError | TypeError | 0j | TypeError| 0j |
|
||||
|
||||
#### The "==" operator case:
|
||||
##### The "==" operator case:
|
||||
|
||||
| | **'0'** | **b'0'** | **0** | **0.0**| **0j** |
|
||||
|---|---------|---------|---------|---------|---------|
|
||||
@@ -55,7 +55,7 @@ The overall return value of each expression is the value assigned to the corresp
|
||||
| **0.0**| False | False | True | True| False |
|
||||
| **0j** | False | False | True | False| True |
|
||||
|
||||
#### 4. Write a small program that uses the AST module to open the lexer/parser example, and dumps the resulting AST. See if there are any arguments to the parse method which make the output easier to read.
|
||||
##### 4. Write a small program that uses the AST module to open the lexer/parser example, and dumps the resulting AST. See if there are any arguments to the parse method which make the output easier to read.
|
||||
|
||||
```
|
||||
import ast
|
||||
@@ -71,13 +71,13 @@ tree = ast.parse(source)
|
||||
print(ast.dump(tree, indent=4))
|
||||
```
|
||||
|
||||
#### 5.What does this do? `print("\007")` Can you figure out why?
|
||||
##### 5.What does this do? `print("\007")` Can you figure out why?
|
||||
|
||||
The statement `print("\007")` prints **a bell character to the console**. The `“\007”` escape sequence represents **the ASCII code for the bell character**, which is also known as the alert or bell control character.
|
||||
|
||||
When this character is printed to a console or terminal, it can produce **an audible bell sound or visual flash**, depending on the configuration of the console or terminal. The behavior can **vary depending on the operating system and the type of console or terminal being used**.
|
||||
|
||||
####6. What does this do? `print("abcdefg\rhijklmnop")` Can you figure out why?
|
||||
##### 6. What does this do? `print("abcdefg\rhijklmnop")` Can you figure out why?
|
||||
|
||||
The code `print("abcdefg\rhijklmnop")` **will output `"hijklmnopg"` on the console**, replacing the first character of `"abcdefg"` with the character `'h'`. The reason for this is the `'\r'` character in the middle of the string.
|
||||
|
||||
@@ -85,7 +85,7 @@ In Python, **the `'\r'` character is a carriage return character**, which instru
|
||||
|
||||
So in this case, the string "abcdefg" is printed, then **the `'\r'` character moves the cursor back to the beginning of the line**, and the string "hijklmnop" is printed, **overwriting the first character of `"abcdefg"` with the character `'h'`**. This results in the output `"hijklmnopdefg"` being printed to the console or terminal.
|
||||
|
||||
####7. This is an HTTP server. Run it and connect to http://localhost:8000 . Why doesn't this do the same thing as what you see above? (don't forget to shut down the HTTP server afterward.)
|
||||
##### 7. This is an HTTP server. Run it and connect to http://localhost:8000 . Why doesn't this do the same thing as what you see above? (don't forget to shut down the HTTP server afterward.)
|
||||
|
||||
```
|
||||
import http.server
|
||||
@@ -107,14 +107,14 @@ print(f"Serving on port {PORT}")
|
||||
|
||||
httpd.serve_forever()
|
||||
```
|
||||
#### Answer:
|
||||
##### Answer:
|
||||
The code provided is a simple HTTP server implementation that serves requests on port 8000. However, the output_string variable in the `do_GET()` method contains the carriage return character `\r`, which could cause issues with the way the server handles the response.
|
||||
|
||||
Specifically, when the output_string is sent to the client, the carriage return character may be interpreted as a newline character, which could break the formatting of the response or cause unexpected behavior.
|
||||
|
||||
To fix this issue, we can remove the carriage return character from the `output_string` variable, or replace it with a different character that doesn't have this issue.
|
||||
|
||||
##### For example:
|
||||
###### For example:
|
||||
```
|
||||
import http.server
|
||||
import socketserver
|
||||
@@ -136,19 +136,19 @@ httpd.serve_forever()
|
||||
```
|
||||
|
||||
|
||||
####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. Hint: applications usually output on web pages, but developers and system administrators usually troubleshoot in the console.
|
||||
##### 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. Hint: applications usually output on web pages, but developers and system administrators usually troubleshoot in the console.
|
||||
|
||||
#### Answer:
|
||||
##### Answer:
|
||||
One way someone might try to hide an embezzlement scheme using the answers to the above question is by using the `"\r"` character to overwrite the console output of a financial transaction.
|
||||
|
||||
For example, if a banking application outputs transaction details to a web page, an attacker might use a script or tool to intercept the response and replace the transaction amount with a smaller value, followed by the `"\r"` character, and then a larger value. When a developer or system administrator tries to troubleshoot the issue by viewing the console output, they will see the larger value and assume that the transaction was processed correctly, while in reality the attacker has embezzled funds by manipulating the output.
|
||||
|
||||
####9. Think of how you would do input validation to prevent such a crime. Replace the code REPLACE ME here so the tests pass:
|
||||
##### 9. Think of how you would do input validation to prevent such a crime. Replace the code REPLACE ME here so the tests pass:
|
||||
|
||||
#### Answer:
|
||||
##### Answer:
|
||||
To prevent the use of the `"\r"` character for hiding malicious input, we can modify the `validate()` function to explicitly disallow it:
|
||||
|
||||
##### An example of the `validate()` function:
|
||||
###### An example of the `validate()` function:
|
||||
```
|
||||
def validate(input_string):
|
||||
if "\r" in input_string:
|
||||
|
||||
Reference in New Issue
Block a user