Skip to content

Instantly share code, notes, and snippets.

@AbeIka
Created October 16, 2016 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AbeIka/005483cdaf6ba66cba51dcbde608b7f7 to your computer and use it in GitHub Desktop.
Save AbeIka/005483cdaf6ba66cba51dcbde608b7f7 to your computer and use it in GitHub Desktop.
import re
# Regular Expressions defining some messages from the server.
acceptsyntax = re.compile ("^ACC\s+(.*?)\s*$")
optionsyntax = re.compile ("^(\w+):\s*(.*?)\s*$")
virussyntax = re.compile ("^VIRUS\s+(\S+)\s+(.*)")
typesyntax = re.compile ("^TYPE\s+(\w+)")
donesyntax = re.compile ("^DONE\s+(\w+)\s+(\w+)\s+(.*?)\s*$")
eventsyntax = re.compile ("^([A-Z]+)\s+(\w+)")
# Receives a line of text from the socket
# \r chars are discarded
# The line is terminated by a \n
# NUL chars indicate a broken socket
def receiveline(s):
line = ''
done = 0
while (not done):
c = s.recv(1)
if (c == ''):
return ''
done = (c == '\n')
if (not done and c != '\r'):
line = line + c
return line
# Receives a whole message. Messages are terminated by
# a blank line
def receivemsg(s):
response = []
finished = 0
while not finished:
msg = receiveline(s)
finished = (len(msg) == 0)
if not finished:
response.append (msg)
return response
# Receives the ACC message which is a single line
# conforming to the acceptsyntax RE.
def accepted(s):
acc = receiveline(s)
return acceptsyntax.match(acc)
# Reads a message which should be a list of options
# and transforms them into a dictionary
def readoptions(s):
resp = receivemsg(s)
opts ={}
for l in resp:
parts = optionsyntax.findall(l)
for p in parts:
if not opts.has_key(p[0]):
opts[p[0]] = []
opts[p[0]].append(p[1])
return opts
# Performs the initial exchange of messages.
def exchangeGreetings(s):
line = receiveline (s)
if (not line.startswith ('OK SSSP/1.0')):
return 0
s.send ('SSSP/1.0\n')
if not accepted(s):
print "Greeting Rejected!!"
return 0
return 1
# performs the final exchange of messages
def sayGoodbye (s):
s.send('BYE\n')
receiveline(s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment