30 lines
1.0 KiB
Python
30 lines
1.0 KiB
Python
|
import random
|
||
|
import threading
|
||
|
import socket
|
||
|
|
||
|
class LocalServer(threading.Thread):
|
||
|
def __init__(self, sharedBuffer):
|
||
|
threading.Thread.__init__(self)
|
||
|
self.sharedBuffer = sharedBuffer
|
||
|
|
||
|
def run(self):
|
||
|
sockfd = socket.socket()
|
||
|
print("Socket successfully created")
|
||
|
port = 12345 # 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 True:
|
||
|
client, addr = sockfd.accept() # Establish connection with client
|
||
|
print('Got connection from', addr)
|
||
|
rcvStr = client.recv(1024).decode()
|
||
|
print(rcvStr)
|
||
|
|
||
|
self.sharedBuffer.push(rcvStr)
|
||
|
|
||
|
client.send(b'') # Send a message to the client
|
||
|
client.close()
|
||
|
print("Socket is listening")
|