48 lines
1.0 KiB
Python
48 lines
1.0 KiB
Python
from socket import *
|
|
from _thread import start_new_thread
|
|
|
|
VERSION = "PYCHAT_0.0"
|
|
PORT = 6783
|
|
|
|
s = socket(AF_INET, SOCK_DGRAM)
|
|
s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
|
|
s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
|
|
s.bind(('', PORT))
|
|
|
|
def recieveThread():
|
|
while True:
|
|
b, a = s.recvfrom(4096)
|
|
if len(b) < 1:
|
|
print(a)
|
|
break
|
|
print(b.decode("utf-8"))
|
|
|
|
|
|
name = ''
|
|
while name == '':
|
|
print("Enter username:", end="")
|
|
name = input()
|
|
if(len(name) > 20):
|
|
print("Error: username must be less than 20 characters")
|
|
name = None
|
|
name = name.lstrip()
|
|
name = name.rjust(19)
|
|
|
|
def sendThread():
|
|
while True:
|
|
#print(name + ' > ', end='')
|
|
msg = name + ' | ' + input()
|
|
s.sendto(msg.encode('utf-8'), ('255.255.255.255', PORT))
|
|
|
|
start_new_thread(recieveThread, ())
|
|
#start_new_thread(sendThread, ())
|
|
try:
|
|
sendThread()
|
|
except KeyboardInterrupt:
|
|
print("Closing socket")
|
|
s.close()
|
|
except EOFError:
|
|
print("Closing socket")
|
|
s.close()
|
|
|