Update page 'Day 2 homework'
+18
-16
@@ -13,27 +13,29 @@
|
|||||||
5. What does this do? `print("\007")` Can you figure out why?
|
5. What does this do? `print("\007")` Can you figure out why?
|
||||||
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?
|
||||||
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
|
import http.server
|
||||||
import socketserver
|
import socketserver
|
||||||
from urllib.parse import urlparse, parse_qs
|
from urllib.parse import urlparse, parse_qs
|
||||||
|
|
||||||
class MyRequestHandler(http.server.SimpleHTTPRequestHandler):
|
class MyRequestHandler(http.server.SimpleHTTPRequestHandler):
|
||||||
def do_GET(self):
|
def do_GET(self):
|
||||||
self.send_response(200)
|
self.send_response(200)
|
||||||
self.send_header('Content-type', 'text/plain')
|
self.send_header('Content-type', 'text/plain')
|
||||||
self.end_headers()
|
self.end_headers()
|
||||||
output_string = "abcdefg\rhijklmnop"
|
output_string = "abcdefg\rhijklmnop"
|
||||||
self.wfile.write(output_string.encode())
|
self.wfile.write(output_string.encode())
|
||||||
|
|
||||||
PORT = 8000
|
PORT = 8000
|
||||||
Handler = MyRequestHandler
|
Handler = MyRequestHandler
|
||||||
httpd = socketserver.TCPServer(("", PORT), Handler)
|
httpd = socketserver.TCPServer(("", PORT), Handler)
|
||||||
print(f"Serving on port {PORT}")
|
print(f"Serving on port {PORT}")
|
||||||
|
|
||||||
httpd.serve_forever()
|
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.
|
|
||||||
|
9. 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.
|
||||||
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:
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user