lessons period 2

This commit is contained in:
2023-12-05 13:50:23 +01:00
parent c9deff7fda
commit cb70a03d81
37 changed files with 1518 additions and 0 deletions

View File

@@ -0,0 +1,31 @@
import socket
HOST = socket.gethostbyname("localhost")
PORT = 5052
def send(msg):
# create a message input
message = msg.encode("utf-8")
# create and bind socket
socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.connect((HOST, PORT))
# send message
socket.send(b'-' * (64 - len(str(len(message)).encode("utf-8"))))
socket.send(message)
socket.recv(1024)
print("Message sent")
socket.close()
message = input("Please enter a message: ")
if message == "":
send("!DISCONNECT")
else:
send(message)

View File

@@ -0,0 +1,34 @@
import socket
# Get local machine name
HOST = socket.gethostname("localhost")
PORT = 5050
def handleClient(clientsocket):
while True:
# Receive no more than 1024 bytes
msg = clientsocket.recv(1024)
if not msg:
break
clientsocket.send(msg)
clientsocket.close()
def start():
# Create a socket object
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind to the port
serversocket.bind((HOST, PORT))
# Queue up to 5 requests
serversocket.listen(5)
while True:
# Establish a connection
clientsocket, addr = serversocket.accept()
print("Got a connection from %s" % str(addr))
msg = 'Thank you for connecting' + "\r\n"
clientsocket.send(msg.encode('ascii'))
clientsocket.close()