Class 6 Lab 4 ‐ Login - Justin-Boyd/Python-Class GitHub Wiki
Task 1: Server Socket Creation
Step 1
- In the PyCharm project created in L1, create a new Python file by right-clicking the projects folder, and selecting New > Python File. Name the file Server Echo.
Step 2
- Import the socket module to the file.
import socket
Step 3
- Create a socket variable.
s = socket.socket()
Step 4
- Bind the socket to accept connections from all IP addresses on port 4444.
s.bind(("0.0.0.0", 4321))
Step 5
- Allow only one connection to the socket.
s.listen(1)
Step 6
- Accept a connection from clients and save the connection object and address to variables.
conn, addr = s.accept()
Step 7
- Send a welcome message to the connected client, and request for a username.
conn.send("Welcome to the server!\nPlease insert your Username:".encode())
Step 8
- Accept the username sent by the client and save it to a variable.
username = conn.recv(2048).decode()
Step 9
- Request a password from the client.
conn.send("Please insert the Password:".encode())
Step 10
- Accept the password sent by the client and save it to a variable.
password = conn.recv(2048).decode()
Step 11
- Create a condition to check if the username is John and the password is 12345.
if username == "John" and password == "12345":
Step 12
- If the username and password are correct, send the client a welcome message.
conn.send(f"Welcome {username}".encode())
Step 13
- If the credentials are incorrect, send a message that tells the client that the password is wrong.
else:
conn.send("Wrong username or password".encode())
Step 14
s.close()
Task 2: Client Socket Creation
Step 1
- In the PyCharm project created in L1, create a new Python file by right-clicking the projects folder, and selecting New > Python File. Name the file Client Echo.
Step 2
- Import the socket module to the file.
import socket
Step 3
- Create a socket variable.
s = socket.socket()
Step 4
- Connect the socket to the listener in the local host.
s.connect(("127.0.0.1", 4321))
Step 5
- Print the first welcome message from the server.
print(s.recv(2048).decode())
Step 6
- Get an input for the username and send it to the server.
s.send(input("").encode())
Step 7
- Print the second message from the server that requests the password.
print(s.recv(2048).decode())
Step 8
- Get an input for the password and send it to the server.
s.send(input("").encode())
Step 9
- Print the final answer from the server regarding authentication
print(s.recv(2048).decode())
Step 10
s.close()
Step 11
- Execute the server script.
Step 12
- Execute the client script.
Final Code
Client Message
"""Lab Objective: Create a login interface using sockets."""
import socket
s = socket.socket()
s.connect(("127.0.0.1", 4321))
print(s.recv(2048).decode())
s.send(input("").encode())
print(s.recv(2048).decode())
s.send(input("").encode())
print(s.recv(2048).decode())
s.close()
input("\nPress 'Enter' to exit the program")# prevents program from closing upon execution
Server Message
"""Lab Objective: Create a login interface using sockets."""
import socket
s = socket.socket()
s.bind(("0.0.0.0", 4321))
s.listen(1)
conn, addr = s.accept()
conn.send("Welcome to the server!\nPlease insert your Username:".encode())
username = conn.recv(2048).decode()
conn.send("Please insert the Password:".encode())
password = conn.recv(2048).decode()
if username == "John" and password == "12345":
conn.send(f"Welcome {username}".encode())
else:
conn.send("Wrong username or password".encode())
s.close()
input("\nPress 'Enter' to exit the program")# prevents program from closing upon execution