functionality before post topics

This commit is contained in:
DBras 2024-06-13 14:45:18 +02:00
parent 7685c36677
commit 5c488235d3
3 changed files with 65 additions and 0 deletions

26
twitter/listener.py Normal file
View File

@ -0,0 +1,26 @@
import sys
import zmq
SERVER_ADDR = 'localhost'
SERVER_PORT = int('5556')
if len(sys.argv) > 1:
port = int(sys.argv[1])
# Socket to talk to server
context = zmq.Context()
socket = context.socket(zmq.SUB)
print(f"Collecting updates from time server at tcp://localhost:{SERVER_PORT}")
socket.connect(f"tcp://{SERVER_ADDR}:{SERVER_PORT}")
topicfilter = 'POST'
socket.setsockopt_string(zmq.SUBSCRIBE, topicfilter)
while True:
try:
incoming = socket.recv_string()
topic, message = incoming.split(';')
print(f'New post from user ' + message)
except KeyboardInterrupt:
print('Closing listener...')
break

10
twitter/poster.py Normal file
View File

@ -0,0 +1,10 @@
from xmlrpc.client import ServerProxy
import sys
if __name__ == '__main__':
username = input('Username: ')
with ServerProxy('http://localhost:9000') as proxy:
while True:
message = input('Message: ')
remote_call = proxy.post(message, username)
print(remote_call)

29
twitter/server.py Normal file
View File

@ -0,0 +1,29 @@
import zmq
import sys
from xmlrpc.server import SimpleXMLRPCServer
PUB_PORT = int('5556')
RPC_PORT = int('9000')
POST_TOPIC = 'POST'
if len(sys.argv) > 1:
PUB_PORT = int(sys.argv[1])
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind(f"tcp://*:{PUB_PORT}")
def post(msg, user):
socket.send_string(f"{POST_TOPIC};{user}: {msg}")
print(f"Published post {user}: {msg}")
return 'Success'
if __name__ == "__main__":
server = SimpleXMLRPCServer(('localhost', RPC_PORT), logRequests=True)
server.register_function(post, 'post')
try:
print("Use Control-C to exit")
server.serve_forever()
except KeyboardInterrupt:
print("Exiting")