Strings in Python

What You Need

Any computer with Internet access.

Purpose

Learn very basic Python strings and networking techniques.

Task 1: Hello, World!

Windows Users

Installing Python

Follow these instructions to install Python 2.7:

https://samsclass.info/124/proj14/python2.7-win.htm

Creating a Python Program

From your desktop, click Start and type CMD.
Right-click "Command Prompt" and click "Run as Administrator".
If a User Account Control box pops up, click Yes.

In the Administrator Command Prompt window, execute these commands, as shown below.

cd c:\python27
notepad hello.py
A box pops up asking "Do you want to create a new file?". Click Yes.

In Notepad, enter this code, as shown below.

print "Hello, World"

In Notepad, click File, Save to save the file.

Running the Python Program

In the Command Prompt, execute this command, as shown below.
python hello.py
The program runs, printing out "Hello, World!", as shown below.

Mac or Linux Users

Creating a Python File

Open a Terminal window and execute this command, as shown below.
nano hello.py
In nano, enter this code, as shown below.
print "Hello, World"

Press Ctrl+X, Y, Enter to save the file.

Running the Python File

In the Terminal window, execute this command, as shown below.
python hello.py
The program runs, printing out "Hello, World!", as shown below.

If this fails, because you are running Python 3, try this command line instead:

/usr/bin/python hello.py


Task 2: Using Strings

Creating the strings.py Program

In a text editor, create a file named greet.py containing this code, as shown below:
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]
Run the program, as shown below.

Examine the output to make sure you understand how to cut out portions of a string, and how to find substrings within a string,


Task 3: Using an Echo Server

Creating the echo.py Program

In a text editor, create a file named echo.py containing this code, as shown below:
import socket
s = socket.socket()
s.connect(("ad.samsclass.info", 10101))
print s.recv(1024)
s.send("Hello from Python!\n")
print s.recv(1024)
s.close()

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", 10101)) connect to the server "ad.samsclass.info" on port 10101
 s.recv(1024) receive data from the server, up to a maximum of 1024 characters
 s.send("Hello from Python!\n") send data to the server
 s.close() close the connection and destroy the "s" object
Run the program, as shown below. It sends the string "Hello from Python!" to the server, which echoes it back.

Challenge 1: Goodbye (5)

Connect to the ad.samsclass.info server on port 10102.

Send it the string "Goodbye".

When you do, you will receive a flag, covered by a green box in the image below.

Enter the flag into the form below to put your name on the WINNERS PAGE!
Name:
Flag:

Troubleshooting

If a service is unresponsive on my server, try adding 10 or 20 to the port number.

There are three copies of each service running:

  • Task 3: Echo -- ports 10101, 10111, 10121
  • Challenge 1: Goodbye -- ports 10102, 10112, 10122
  • Challenge 2: Increment -- ports 10103, 10113, 10123
  • Challenge 4: Add and Subtract -- ports 10104, 10114, 10124

Challenge 2: Increment (10)

Connect to the ad.samsclass.info server on port 10103.

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

Integers and strings are different data types in Python.

You can convert items back and forth with the int() and str() functions, as shown below.

Enter the flag into the form below to put your name on the WINNERS PAGE!
Name:
Flag:

Challenge 3: Add and Subtract (25)

Connect to the ad.samsclass.info server on port 10104.

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.

Enter the flag into the form below to put your name on the WINNERS PAGE!
Name:
Flag:


Posted: 10-17-18

Leetspeak typo fixed, additional ports added 10-18-18
/usr/bin/python Mac tip added, thanks to a @hackvalley27 participant 10-21-18