Hack My Planet
Day 14: Static IP & Client/Server Chat
Opening Comments
- Today will be composed of team challenges.
- Don't be afraid to ask for help if you don't understand something.
- IF SOMETHING DOESN'T WORK, DOUBLE CHECK YOUR CODE. IF YOU ASK FOR HELP AND IT'S A SYNTAX ERROR, WE WILL NOT HELP YOU.
Python Review
- Loops
- Functions
- Parameters
- White space
- Return()
Function Review
Define a function overlapping() that takes two lists as arguments (parameters) and returns True if they have at least one member in common, False otherwise.
Note: Write this using a nested for-loop
Static IP
- Dynamic vs Static
- Instructions:
- Add the following lines at the bottom of the file:
- Note: X is your assigned number; Ctrl+O to save, Ctrl+X to exit
sudo nano /etc/dhcpcd.conf
interface eth0
static ip_address=192.168.0.X/24
static routers=192.168.0.1
static domain_name_servers=192.168.0.1
- Testing:
- s
- i
- You should see the IP address you set in the eth0: entry
sudo reboot
ip a
Preparation
- Do all of the following exercises and slides in the directory ~/Documents/python_work
- Download the network.py module with:
wget https://goo.gl/UJMdZh -O network.py --no-check-certificate
Setting Up a Chat Network
- Network your Pi with your neighbor's to test the connection
- s
- IP_ADDRESS = your neighbor's static IP
- DON'T ACTUALLY TYPE IP_ADDRESS
- s
- Set up the chat program
- Using Geany, create the file 'chat.py'
- Code is on the following slide
sudo ping IP_ADDRESS -c5
chat.py
# A simple internet-chat application
import network
import sys
def heard(phrase):
print("them: " + phrase)
if (len(sys.argv) >= 2):
network.call(sys.argv[1], whenHearCall=heard)
else:
network.wait(whenHearCall=heard)
while network.isConnected():
phrase = input()
print("me: " + phrase)
network.say(phrase)
Chatting
- Set up the server Pi with:
- Set up the client Pi with:
python3 chat.py
python3 chat.py SERVER_IP
SERVER_IP is the server's static IP address, DON'T ACTUALLY TYPE SERVER_IP
- Type out a message and press Enter
Editing the Source Code
- Customize your chat by playing around with the code
- Some examples are:
- Display a welcome message when your program starts
- Display a welcome message for your caller when they connect
- When the word "random" is typed, send one of a number of different random messages to your caller (Hint: look at Magic Eight Ball source code)
Hack My Planet Day 14 Static IP & Networking Chat
By jtheadland
Hack My Planet Day 14 Static IP & Networking Chat
- 538