isengard-bot/localServer.py

34 lines
1.1 KiB
Python
Raw Normal View History

2022-02-05 00:46:57 +01:00
import random
import threading
import socket
class LocalServer(threading.Thread):
def __init__(self, sharedBuffer):
threading.Thread.__init__(self)
self.sharedBuffer = sharedBuffer
2022-02-07 18:39:34 +01:00
self.pleaseStop = False
2022-02-05 00:46:57 +01:00
def run(self):
2022-02-06 01:29:59 +01:00
2022-02-05 00:46:57 +01:00
sockfd = socket.socket()
print("Socket successfully created")
2022-02-06 01:29:59 +01:00
port = 12346 # Reserve a port on your computer...in our case it is 12345, but it can be anything
2022-02-05 00:46:57 +01:00
sockfd.bind(('127.0.0.1', port))
print("Socket binded to %s" %(port))
sockfd.listen(5) # Put the socket into listening mode
print("Socket is listening")
2022-02-07 18:39:34 +01:00
while not self.pleaseStop:
2022-02-05 00:46:57 +01:00
client, addr = sockfd.accept() # Establish connection with client
print('Got connection from', addr)
rcvStr = client.recv(1024).decode()
print(rcvStr)
2022-02-07 18:39:34 +01:00
if rcvStr != '':
self.sharedBuffer.push(rcvStr)
2022-02-05 00:46:57 +01:00
client.send(b'') # Send a message to the client
client.close()
print("Socket is listening")
2022-02-07 18:39:34 +01:00
print("Socket is closed\nExiting")