https://colab.research.google.com/If you see a blue "Sign In" button at the top right, click it and log into a Google account.
From the menu, click File, "New notebook".
This project was written assuming you are using Google Colab. If you want to see the old version, which assumed you were running a local installation of Python, go here.
greet = "Hello, World!"
print(greet)
print("Start: ", greet[0:3])
print("Middle: ", greet[3:6])
print("End: ", greet[-3:])
a = greet.find(",")
print("Portion before comma", greet[:a])
Examine the output to
make sure you understand how to cut out
portions of a string, and how to find
substrings within a string.
Here is a handy reference:
Execute this code, as shown below:
import socket
s = socket.socket()
s.connect(("ad.samsclass.info", 10201))
r = s.recv(1024).decode()
print(r)
s.send("Hello from Python!\n".encode())
r = s.recv(1024).decode()
print(r)
print("First five letters of response:", r[0:5])
s.close()
Run the program, as shown below. It sends the string "Hello from Python!" to the server, which echoes it back.
Explanation
import socket import the "socket" library, which contains networking functions s = socket.socket() create a socket object named "s" s.connect(("ad.samsclass.info", 10201)) connect to the server "ad.samsclass.info" on port 10201 s.recv(1024) receive data from the server, up to a maximum of 1024 characters s.send("Hello from Python!\n".encode()) send data to the server s.close() close the connection and destroy the "s" object
Flag VP 100.1: Goodbye (5 pts)
Connect to the ad.samsclass.info server on port 10202.Send it the string "Goodbye".
When you do, you will receive a flag, covered by a green box in the image below.
Flag VP 100.2: Increment (10)
Connect to the ad.samsclass.info server on port 10203.It sends you a number. Add one to that number and send it to the server.
When you do, you will receive a flag, covered by a green box in the image below.
Hint: Python String to Int() Tutorial
for i in range(3):
print(i)
print()
for c in "CAT":
print(c)
Observe how simple
loops work in Python,
as shown below.
Notice that a colon starts a loop, and that every statement in a loop must be indented.
You can read more about loops in this tutorial.
Flag VP 100.3: Add and Subtract (25)
Connect to the ad.samsclass.info server on port 10204.Combine two numbers as required and send the result.
You have to get all five answers correct within five seconds to get the flag, covered by a green box in the image below.
Hint: Only connect once. If you connect five times, you'll always be solving the first challenge, and never see the flag.
So in Python 2, you can store data like this:
Let's see how that works in Python 3.Execute this code:
a = '\x41\x42\xff'
print(a)
print(a[0])
print(a[1])
print(a[2])
The third character now appears as a
printable character, but a non-English one,
as shown below.
To see the bytes used to store the string,
execute these commands:
b = a.encode()
print(hex(b[0]))
print(hex(b[1]))
print(hex(b[2]))
print(hex(b[3]))
There are 4 bytes in the encoded
object, not three, and none of them
are ff,
as shown below.
What's going on?
So the character '\xff' in the string is interpreted a Unicode Code Point, and it's stored in two bytes, as shown below:
UTF-8 characters have variable length, from one to four bytes. This means they can be used to print a lot of useful characters,Execute these commands:
print('\x41')
print('\U00000041')
print('\x41'.encode())
print('\U00000041'.encode())
print('\x41'.encode().hex())
print('\U00000041'.encode().hex())
'\x41' and
'\U00000041' are valid
representations of a character,
and they are equivalent.
Python requires all four bytes to be
specified for every Unicode character,
even when some of them are leading zeroes.
Execute these commands:
print('\xff')
print('\U000000ff')
print('\xff'.encode())
print('\U000000ff'.encode())
'\xff' and '\U000000ff' are both valid ways
to refer to Code Point FF, which is encoded
in two bytes.
Unicode code point values are not identical to their binary representations in memory.
Flag VP 100.4: Greek (5)
Find the eight-character Unicode code point for a capital Sigma, covered by a green box in the image below.
Flag VP 100.5: Burgertime (5)
Find the eight-character Unicode code point for a hamburger, covered by a green box in the image below.