Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
45 lines (33 sloc) 1.47 KB
def hello(ip,port,sockobj, thiscommhandle,listencommhandle):
sockobj.recv(512) # Receive HTTP header
mycontext['pagecount'] = mycontext['pagecount'] + 1
htmlresponse = "<html><head><title>Hello World</title></head>" + \
"<body><h1> Hello World!</h1><p>You are visitor " + \
str(mycontext['pagecount']) + "</p></body></html>"
sockobj.send("HTTP/1.1 200 OK\r\nContent-type: text/html\r\n" + \
"Content-length: %i\r\n\r\n%s" % (len(htmlresponse), htmlresponse))
stopcomm(thiscommhandle) # close my connection with this user
# stop the previous timer...
canceltimer(mycontext['stopevent'])
# start a new one for 10 seconds from now...
eventhandle = settimer(10, stop_listening, (listencommhandle,))
mycontext['stopevent'] = eventhandle
def stop_listening(commhandle):
stopcomm(commhandle) # this will deregister hello
if callfunc == 'initialize':
mycontext['pagecount'] = 0
if len(callargs) > 1:
raise Exception("Too many call arguments")
# Running remotely:
# whenever this vessel gets a connection on its IPaddress:GENIport it'll call hello
elif len(callargs) == 1:
port = int(callargs[0])
ip = getmyip()
# Running locally:
# whenever we get a connection on 127.0.0.1:12345 we'll call hello
else:
port = 12345
ip = '127.0.0.1'
listencommhandle = waitforconn(ip,port,hello)
eventhandle = settimer(10, stop_listening, (listencommhandle,))
mycontext['stopevent'] = eventhandle