from socket import *
import thread
BUFF = 1024
HOST = '127.0.0.1'# must be input parameter @TODO
PORT = 9999 # must be input parameter @TODO
def gen_response():
return 'this_is_the_return_from_the_server'
def handler(clientsock,addr):
while 1:
data = clientsock.recv(BUFF)
print 'data:' + repr(data)
if not data: break
clientsock.send(gen_response())
print 'sent:' + repr(gen_response())
clientsock.close()
if __name__=='__main__':
ADDR = (HOST, PORT)
serversock = socket(AF_INET, SOCK_STREAM)
serversock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
serversock.bind(ADDR)
serversock.listen(5)
while 1:
print 'waiting for connection...'
clientsock, addr = serversock.accept()
print '...connected from:', addr
thread.start_new_thread(handler, (clientsock, addr))
Diff to Previous Revision
--- revision 2 2012-09-19 17:57:49
+++ revision 3 2012-09-19 17:59:01
@@ -4,7 +4,8 @@
BUFF = 1024
HOST = '127.0.0.1'# must be input parameter @TODO
PORT = 9999 # must be input parameter @TODO
-def gen_response(key):
+
+def gen_response():
return 'this_is_the_return_from_the_server'
def handler(clientsock,addr):
@@ -12,8 +13,8 @@
data = clientsock.recv(BUFF)
print 'data:' + repr(data)
if not data: break
- clientsock.send(response)
- print 'sent:' + repr(response)
+ clientsock.send(gen_response())
+ print 'sent:' + repr(gen_response())
clientsock.close()
if __name__=='__main__':