import socket

# Change the following host and see what IP it prints!
host = "google.com"
ip = socket.gethostbyname(host)

print(ip)
142.250.176.14
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.connect((ip, 80))
    print("Successfully connected!")
Successfully connected!

Check-In

  1. What is an IP address? Unique identifier for a device on Internet.
  2. What is a TCP port? Unique identifier for an application on a device.
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.connect((ip, 80))

    # Send a GET request to "/"
    s.sendall(b"GET / HTTP/1.1\r\n\r\n")

    # Recieve & print 2048 bytes of data
    data = s.recv(2048)
    print(data.decode())
HTTP/1.1 200 OK
Date: Wed, 26 Apr 2023 21:06:50 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
Content-Security-Policy-Report-Only: object-src 'none';base-uri 'self';script-src 'nonce-tWlxOx6t94Dd9xmLup9sAw' 'strict-dynamic' 'report-sample' 'unsafe-eval' 'unsafe-inline' https: http:;report-uri https://csp.withgoogle.com/csp/gws/other-hp
P3P: CP="This is not a P3P policy! See g.co/p3phelp for more info."
Server: gws
X-XSS-Protection: 0
X-Frame-Options: SAMEORIGIN
Set-Cookie: 1P_JAR=2023-04-26-21; expires=Fri, 26-May-2023 21:06:50 GMT; path=/; domain=.google.com; Secure
Set-Cookie: AEC=AUEFqZchFqKN74ejyNMr7lczJ9yDs5GTWkNlKeYpkgSetxu-8ppfdaPjgA; expires=Mon, 23-Oct-2023 21:06:50 GMT; path=/; domain=.google.com; Secure; HttpOnly; SameSite=lax
Set-Cookie: NID=511=E-0j8XfQq_RWf7f9uoCth1cjTjuoR-HX7eUVUMKdFREAxvOfF35Tnl1s6an0MhGHnZyKtQqiP4QJVEZ9oMMdP9DcvZgvFLfRx7Afmbxu0yQPiJPvP2wJ5E8vMI2q_cEcs1W2T7Y4gz417x5IOdonr6SrXD2uBc_U2rvBFXvQ0Zo; expires=Thu, 26-Oct-2023 21:06:50 GMT; path=/; domain=.google.com; HttpOnly
Accept-Ranges: none
Vary: Accept-Encoding
Transfer-Encoding: chunked

5b46
<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en"><head><meta content="Search the world's information, including webpages, images, videos and more. Google has many special features to help you find exactly what you're looking for." name="description"><meta content="noodp" name="robots"><meta content="text/html; charset=UTF-8" http-equiv="Content-Type"><meta content="/images/branding/googleg/1x/googleg_standard_color_128dp.png" itemprop="image"><title>Google</title><script nonce="tWlxOx6t94Dd9xmLup9sAw">(function(){window.google={kEI:'apJJZIGaAuzIkPIPh46f0A0',kEXPI:'0,18167,1341242,1709,4350,206,4804,2316,383,246,5,1129120,1197710,303252,77528,16115,28684,22431,1361,12312,17587,4998,13228,3847,38444,885,1987,2891,3926,7828,606,29842,30848,15324,432,3,346,1244,1,16916,2652,4,1528,2304,29062,13064,11444,2215,2980,1457,16786,5800,2557,4094,7596,1,4215
import requests

# Change the URL to whatever you'd like
response = requests.get("https://google.com")

print("Status code:", response.status_code)
print("Headers:", response.headers)
print("Response text:", response.text[:100])

# Add a line to print the "Content-Type" header of the response
# Try an image URL!
Status code: 200
Headers: {'Date': 'Wed, 26 Apr 2023 21:10:25 GMT', 'Expires': '-1', 'Cache-Control': 'private, max-age=0', 'Content-Type': 'text/html; charset=ISO-8859-1', 'Content-Security-Policy-Report-Only': "object-src 'none';base-uri 'self';script-src 'nonce-6gi9dKt0o9iLwf1TwS_kLg' 'strict-dynamic' 'report-sample' 'unsafe-eval' 'unsafe-inline' https: http:;report-uri https://csp.withgoogle.com/csp/gws/other-hp", 'P3P': 'CP="This is not a P3P policy! See g.co/p3phelp for more info."', 'Content-Encoding': 'gzip', 'Server': 'gws', 'X-XSS-Protection': '0', 'X-Frame-Options': 'SAMEORIGIN', 'Set-Cookie': '1P_JAR=2023-04-26-21; expires=Fri, 26-May-2023 21:10:25 GMT; path=/; domain=.google.com; Secure, AEC=AUEFqZf7_FRkMx1IvkPLsmojxU6o_FrwixaSF-ZUNPFoDYhXkRbH6c_3mxk; expires=Mon, 23-Oct-2023 21:10:25 GMT; path=/; domain=.google.com; Secure; HttpOnly; SameSite=lax, NID=511=dx4Tiy53t8ycg6QOGwuYFIs9kVTIWhdprj1a39J83CBwnx9FbBiFbjSOYTj_Ssi1G6WQ4h8PlSOT7NaqFHoKHmXV3unxw-p37nHub4ah_GdXCYJec0adMUXONiSWq7lLyGoLPyt8xRlmaTcp6V_yvp6LzhrmuteozPe3KS-1-Ds; expires=Thu, 26-Oct-2023 21:10:25 GMT; path=/; domain=.google.com; HttpOnly', 'Alt-Svc': 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000', 'Transfer-Encoding': 'chunked'}
Response text: <!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en"><head><meta content

NGINX

aws = "3.130.255.192"

response = requests.get("http://" + aws)
print(response.text)
<!doctype html>
<html>
<head>
<title>Cool site</title>
<meta name="description" content="cool site for apcsp">
</head>
<body>
Hello, this is my cool site. Check out my products:
<a href="/products">Products!!</a>
</body>
</html>

Configuration

server {
    // Listen on virtual "port 80"
    listen 80;
    listen [::]:80;
    server_name 3.130.255.192;

    location / {
        // Inform server about original client
        proxy_set_header        Host $host;
        proxy_set_header        X-Real-IP $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header        X-Forwarded-Proto $scheme;

        // Forward all requests transparently to the server running on our computer
        proxy_pass              http://localhost:9099;
    }
}

Load Balancing

upstream example.com {
    server server1.example.com;
    server server1.example.com;
}

HTTP Headers

server {
    add_header X-Cool-Header "I love APCSP!";

    location /pages {
        add_header X-Cooler-Header "This is my secret header!";
    }
}

Check In

  1. Research 1 HTTP header and describe, in detail, its purpose.
  2. Write a line in a sample NGINX configuration that will add that specific header to the /information location
  3. Explain the purpose of the load balancing performed by NGINX
  4. Modify the following code block to obtain the value of the secret header on /products of the AWS site
aws = "3.130.255.192"

response = requests.get("http://" + aws+ "/products")

print("The secret header is:", "...")
The secret header is: ...

Hacks

  • Complete the above check-in questions and change the hosts (0.1)
  • Complete the above code-segment to retrieve the secret header (0.1)

Bonus (0.05)

Create a diagram showing the layers of abstraction that allow us to use HTTP (IP, TCP, etc.)

CORS Hacks

  1. Explain what CORS is and what it stands for
  2. Describe how you would be able to implement CORS into your own websites
  3. Describe why you would want to implement CORS into your own websites
  4. How could use CORS to benefit yourself in the future?

Total: 0.2 points

KASM Hacks

  1. What is the purpose of "sudo" when running commands in terminal?
  2. What are some commands which allow us to look at how the storage of a machine is set up as?
  3. What do you think are some alternatives to running "curl -O" to get the zip file for KASM?
  4. What kind of commands do you think the "install.sh" command has and why is it necessary to call it?
  5. Explain in at least 3-4 sentences how deploying KASM is related to/requires other topics talked about in the lesson and/or potential ways to add things mentioned in the lesson to this guide.

Total: 0.2 points

AWS/RDS Hacks

See the setup post

  • Create your own database in the EC2 I have created (ec2-database-connect)
    • name it with your first and last name (example: aditya-nawandhar) (0.1)
    • Create a table using the commands on the link provided. (0.1)
    • using commands from the link provided make columns and rows with test data (can be anything) (example: “name” and “class” are the columns with rows being something like “Aditya” and “Junior”). At least 4 test rows (0.1)
    • additional points if the data matches CPT (Bonus: 0.05)

Total: 0.3