isengard-bot/localServer.py

42 lines
1.2 KiB
Python

import random
import threading
import socket
import logging
from systemd.journal import JournalHandler
# Logging
log = logging.getLogger(__name__)
log.addHandler(JournalHandler())
log.setLevel(logging.INFO)
class LocalServer(threading.Thread):
def __init__(self, sharedBuffer):
threading.Thread.__init__(self)
self.sharedBuffer = sharedBuffer
self.pleaseStop = False
def run(self):
sockfd = socket.socket()
print("Socket successfully created")
port = 12346 # Reserve a port on your computer...in our case it is 12345, but it can be anything
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")
while not self.pleaseStop:
client, addr = sockfd.accept() # Establish connection with client
print('Got connection from', addr)
rcvStr = client.recv(1024).decode()
print(rcvStr)
if rcvStr != '':
self.sharedBuffer.push(rcvStr)
client.send(b'') # Send a message to the client
client.close()
print("Socket is listening")
print("Socket is closed\nExiting")