Created
November 17, 2016 09:05
-
-
Save vedgar/9d2d5aecdae49d99989afd7476bd0d1c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import enum, struct | |
class Poruka(enum.IntEnum): | |
LOGIN = 1 | |
USERS = 2 | |
USERLIST = 3 | |
BYE = 4 | |
NEWMESSAGE = 5 | |
CHECKMESSAGES = 6 | |
MESSAGE = 7 | |
header = struct.Struct('!2L') # network order, two unsigned longs | |
def primi(veza): | |
def primi_točno(duljina): | |
poruka = b'' | |
while len(poruka) < duljina: poruka += veza.recv(duljina - len(poruka)) | |
return poruka | |
duljina, vrsta = header.unpack(primi_točno(header.size)) | |
return primi_točno(duljina).decode('utf-8'), Poruka(vrsta) | |
def pošalji(veza, vrsta, poruka): | |
poruka = poruka.encode('utf-8') | |
print(header.pack(len(poruka), vrsta) + poruka) | |
veza.sendall(header.pack(len(poruka), vrsta) + poruka) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment