This repository was archived by the owner on Aug 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimple-socket-server.py
More file actions
38 lines (33 loc) · 1.46 KB
/
simple-socket-server.py
File metadata and controls
38 lines (33 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import socket
import random
print("---Server---")
HOST, PORT = "127.0.0.1", 3003 # -> 1 to 65535
LIST = ["rock", "paper", "scissors"]
server_choice = random.choice(LIST)
print(f"My choice is {server_choice}")
print("Wait for Client ...")
# socket.AF_INET -> Internet Address Familly for IPv4
# socket.SOCK_STREAM -> Socket Type for TCP
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server_socket:
server_socket.bind((HOST, PORT))
server_socket.listen()
con_socket, ret_address = server_socket.accept()
with con_socket:
client_choice = con_socket.recv(2048).decode()
msg = f"SERVER picked: {server_choice}\nCLIENT picked: {client_choice}"
if server_choice == client_choice:
msg += "\nDRAW"
elif server_choice == "rock" and client_choice == "paper":
msg += "\nWinner is [Client]"
elif server_choice == "rock" and client_choice == "scissors":
msg += "\nWinner is [Server]"
elif server_choice == "paper" and client_choice == "rock":
msg += "\nWinner is [Server]"
elif server_choice == "paper" and client_choice == "scissors":
msg += "\nWinner is [Client]"
elif server_choice == "scissors" and client_choice == "paper":
msg += "\nWinner is [Server]"
elif server_choice == "scissors" and client_choice == "rock":
msg += "\nWinner is [Client]"
print(msg)
con_socket.sendall(msg.encode())