12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- #!/usr/bin/python
- import socket
- import _thread
- import time
- import socket
- import os
- import random
- from padra import padra
- LenFunc = 4
- padra = padra(LenFunc)
- HOST = '127.0.0.1' # Standard loopback interface address (localhost)
- socketPort = int(random.random()*5000+1024)
- def broadcast():
- sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
- sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
- sock.bind(("", 6666))
- while True:
- msg, addr = sock.recvfrom(1024)
- sock.sendto(bytes(str(socketPort),'utf-8'),addr)
- connectedUser = []
- def listen():
- with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
- s.bind((HOST, socketPort))
- s.listen()
- while True:
- conn, addr = s.accept()
- u = _thread.start_new_thread( user, (conn, addr))
- connectedUser.append((addr,u))
- class user():
- def __init__(self,connection,address):
- self.conn = connection
- self.addr = address
- self.history = []
- _thread.start_new_thread( self.receive, ())
- self.send("Connected")
- def send(self,Message):
- Message=bytes(Message,'utf-8')
- self.conn.sendall(Message)
- self.history.append(("send",Message))
- def receive(self):
- while True:
- len = self.conn.recv(LenFunc)
- if not len:
- break
- data = self.conn.recv(int(len))
- padra.parse(data.decode("utf-8"))
- self.history.append(("receive",data))
- try:
- _thread.start_new_thread( broadcast, ())
- _thread.start_new_thread( listen, ())
- except:
- print ("Error: unable to start thread")
- while 1:
- pass
|