Home API Notes
import socket

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

print(ip)
142.250.189.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?

A string of numbers that allow a device to be uniquely identified on the internet.

  1. What is a TCP port?

A TCP port allows two devices to communicate and reassemble data sent to each other through the use of packets. Packets are a collection of bytes, sending them allows two computers to determine whether each packet has been received from a specific location.

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")

    # Receive & print 2048 bytes of data
    data = s.recv(2048)
    print(data.decode())
HTTP/1.1 200 OK
Date: Wed, 26 Apr 2023 21:18:31 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-gRXVZiimWFC3Sk-ep3WaGg' '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:18:31 GMT; path=/; domain=.google.com; Secure
Set-Cookie: AEC=AUEFqZfidJWIAB3S83vWyq-evvkFEK8NZ9Gutxg2O9Cnp5Xl5iXUbsM5nA; expires=Mon, 23-Oct-2023 21:18:31 GMT; path=/; domain=.google.com; Secure; HttpOnly; SameSite=lax
Set-Cookie: NID=511=QNKuTsIXvsyE6PFkMRC8nZV1YXDhy54OPoCgl8s1n5RxvZz7AvQI1A1u9a69a2PrjoqnNp1ch6UAsW7GXNPeAnbFuJAsdpwMn-D7axb5jZ1_NtaURW5qCRdnK6dppCiUVD-igaEIHgImHMJ5kA297Z0BqL3q4myQMQrVxGCRV_k; expires=Thu, 26-Oct-2023 21:18:31 GMT; path=/; domain=.google.com; HttpOnly
Accept-Ranges: none
Vary: Accept-Encoding
Transfer-Encoding: chunked

5b6f
<!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="gRXVZiimWFC3Sk-ep3WaGg">(function(){window.google={kEI:'J5VJZLrbH7LbkPIPpM6voAY',kEXPI:'0,1359409,6058,207,2414,2390,2316,383,246,5,1129120,1197790,380701,16108,19404,9286,22430,1362,12318,17581,4998,13228,3847,38444,2872,2891,3926,4422,3406,606,60690,6515,8809,432,3,1590,1,16916,2652,4,1528,2304,29062,13065,13658,2980,18243,5824,2533,4094,7596,1,11943,30211,2,14022,2373,3
import requests

# Change the URL to whatever you'd like
response = requests.get("https://www.espn.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: {'Content-Type': 'text/html;charset=UTF-8', 'Content-Length': '157340', 'Connection': 'keep-alive', 'Date': 'Fri, 28 Apr 2023 05:37:35 GMT', 'access-control-allow-origin': '*', 'content-security-policy': "frame-ancestors  'self' asia.espn.com:* asia.espnqa.com:* *.espn.com:* *.espnqa.com:* *.espnsb.com:* *.espnsb.com *.espn.co.uk *.espndeportes.espn.com *.espn.com.br *.espn.com.mx *.espn.com.ve *.espn.com.ar *.espn.com.co *.espnfc.com.au *.espn.com.au *.espn.in *.espn.com.sg *.espn.cl *.espn.ph *.espn.ph:* *.espn.com.pe *.espn.com.gt *.espn.com.do *.espn.com.ec *.espn.com.uy *.espn.com.pa *.espn.co.cr qa.abcnews.go.com preview.abcnews.go.com abc7ny.com abc7.com *.abcotvssb.com preview.goodmorningamerica.com http://*.espnqa.com:* http://*.espn.com:* *.abcotvssb.com *.abcnews.go.com *.abcnews.go.com:* http://*.abcnews.go.com:* abc30.com abc7news.com abc13.com abc7chicago.com 6abc.com abc11.com *.goodmorningamerica.com qa.abc7.com qa.abc30.com qa.abc7news.com qa.abc13.com qa.abc7chicago.com qa.6abc.com qa.abc7ny.com qa.abc11.com *.abcnews.go.com:* abcnews.go.com qa.secsports.com *.secsports.com https://*.espn.com:*", 'etag': 'W/"aa4d38d388f5090336c5689e25a377a123b195ca"', 'content-encoding': 'gzip', 'via': '1.1 varnish (Varnish/6.0), 1.1 5b08d9a177789aafebc4cbacd6becc36.cloudfront.net (CloudFront)', 'vary': 'Accept-Encoding', 'accept-ranges': 'bytes', 'set-cookie': 'country=us; path=/;, connectionspeed=full; path=/; Expires=Fri, 05 May 2023 05:37:35 GMT;, edition=espn-en-us; path=/; Expires=Fri, 05 May 2023 05:37:35 GMT;, edition-view=espn-en-us; path=/; Expires=Fri, 05 May 2023 05:37:35 GMT;, region=ccpa; path=/; Expires=Fri, 05 May 2023 05:37:35 GMT;, _dcf=1; path=/; Expires=Fri, 05 May 2023 05:37:35 GMT;, SWID=1E054CE2-D116-4556-C52D-C37E80CFAD9E; path=/; Expires=Tue, 28 Apr 2043 05:37:35 GMT; domain=espn.com;', 'cache-control': 'max-age=0, must-revalidate', 'X-Cache': 'Miss from cloudfront', 'X-Amz-Cf-Pop': 'LAX50-P1', 'X-Amz-Cf-Id': 'tgEJNDyvOzjctQewzDUPxcN6q7PBHlaPkOh0kJnVryuzV7bbyKELaQ=='}
Response text: 
	<!DOCTYPE html>
	<html class="no-icon-fonts" lang="en">
	<head>
		<meta http-equiv="content-type" 

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.
  1. Write a line in a sample NGINX configuration that will add that specific header to the /information location
  1. Explain the purpose of the load balancing performed by NGINX
  1. 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:", response)
The secret header is: <Response [200]>

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)

The secret header is <Response [200]>

Bonus (0.05)

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

Client

CORS Hacks

  1. Explain what CORS is and what it stands for

CORS stands for Cross-Origin Resource Sharing. It is a mechanism that allows restricted resources on a web page to be requested from another domain outside the domain from which the first resource was served. A web page may freely embed cross-origin images, stylesheets, scripts, iframes, and videos.

  1. Describe how you would be able to implement CORS into your own websites

To implement CORS into your own website, you will need to add the following HTTP headers to your server responses:

  • Access-Control-Allow-Origin: This header specifies the origin(s) that are allowed to make cross-origin requests.
  • Access-Control-Allow-Methods: This header specifies the HTTP methods that are allowed for cross-origin requests.
  • Access-Control-Allow-Headers: This header specifies the HTTP headers that are allowed to be sent with cross-origin requests.
  • Access-Control-Expose-Headers: This header specifies the HTTP headers that are allowed to be exposed in the response of a cross-origin request.
  1. Describe why you would want to implement CORS into your own websites

There are several reasons why one might want to implement CORS into your own website. For example, they might want to allow your users to embed content from other websites, such as social media posts or images. They might also want to allow your users to make AJAX requests to other websites.

  1. How could use CORS to benefit yourself in the future?

CORS can be used to benefit me in the future by allowing me to build more interactive and engaging websites. For example, I could use CORS to allow your users to embed content from other websites, such as social media posts or images. I could also use CORS to allow your users to make AJAX requests to other websites.

Total: 0.2 points

KASM Hacks

  1. What is the purpose of "sudo" when running commands in terminal?

The purpose of the sudo command is to run commands as the root user. This is necessary for certain tasks, such as installing software or making changes to system files, because these tasks require elevated privileges.

  1. What are some commands which allow us to look at how the storage of a machine is set up as?

Some commands which allow us to look at how the storage of a machine is set up as include:

  • df -h: This command displays a summary of the file system disk space usage.
  • lsblk: This command displays a list of block devices, including their size, type, and mount point.
  • fdisk -l: This command displays a list of all partitions on a disk.
  1. What do you think are some alternatives to running "curl -O" to get the zip file for KASM?

Some alternatives to running curl -O to get the zip file for KASM include:

  • Downloading the zip file from the KASM website manually.
  • Using a web downloader tool, such as Wget or HTTrack.
  • Using a file sharing service, such as Dropbox or Google Drive.
  1. What kind of commands do you think the "install.sh" command has and why is it necessary to call it?

The install.sh command is a shell script that contains the commands necessary to install KASM. These commands may include:

  • Downloading and extracting the KASM zip file.
  • Creating a KASM directory and installing the KASM files into it.
  • Starting the KASM service.
  1. 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.

KASM is designed to be scalable, and deploying it requires planning for future growth. This includes things like sizing the infrastructure appropriately, choosing the right deployment type, and configuring load balancing. KASM can also be used to comply with a variety of industry regulations. Deploying it requires understanding the specific compliance requirements that apply to your organization and configuring KASM accordingly. Some potential ways to add things mentioned in the lesson to this guide include:

  • Adding more detail on the security considerations involved in deploying KASM.
  • Adding more detail on the scalability considerations involved in deploying KASM.

Total: 0.2 points

DNS Hacks

  1. What does DNS stand for?

DNS stands for Domain Name System.

  1. What is the purpose of DNS?

The Domain Name System (DNS) is a hierarchical naming system for computers, services, or any resource connected to the Internet or a private network. It associates various information with domain names assigned to each of the participants. Most prominently, it translates easily memorized domain names to the numerical IP addresses needed for locating and identifying computer services and devices with the underlying network protocols. By translating human-readable hostnames into machine-readable IP addresses, the Domain Name System makes the Internet more user-friendly.

  1. How does DNS work?

DNS works by using a distributed database of DNS records. These records map domain names to IP addresses. When a user types in a domain name, their computer sends a DNS query to a DNS server. The DNS server then looks up the domain name in its database and returns the corresponding IP address. The user's computer then uses the IP address to connect to the website or other online resource.

  1. What is a DNS resolver?

A DNS resolver is a server that performs DNS lookups. When a user types in a domain name, their computer sends a DNS query to a DNS resolver. The DNS resolver then looks up the domain name in its database and returns the corresponding IP address. The user's computer then uses the IP address to connect to the website or other online resource.