We Recommend

Learning Python Learning Python
The authors of Learning Python show you enough essentials of the Python scripting language to enable you to begin solving problems right away, then reveal more powerful aspects of the language one at a time. This approach is sure to appeal to programmers and system administrators who have urgent problems and a preference for learning by semi-guided experimentation.


Posted By

miziomon on 11/17/06


Tagged

socket server


Versions (?)


Who likes this?

7 people have marked this snippet as a favorite

sudarkoff
px
yuconner
anayhk
vali29
fukami
pulczynski


Socket Server


Published in: Python 


URL: http://maurizio.mavida.com

very simple socket server.

  1. import socket
  2. host = "localhost"
  3. port = 12345
  4. threads = 1
  5. start_server = False
  6. try:
  7. SocketServer = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
  8. SocketServer.bind ( ( host , port ) )
  9. SocketServer.listen ( threads )
  10. start_server = True
  11. except e:
  12. print "server error"
  13. print e
  14. while start_server:
  15. channel, details = SocketServer.accept()
  16. print 'open ', details
  17. conn_active = True
  18. while conn_active:
  19. data = channel.recv ( 1024 )
  20. print "-----------------------------"
  21. print "receive"
  22. print "-----------------------------"
  23. print data
  24. print "-----------------------------"
  25. if (data != "quit"):
  26. channel.send ( data )
  27. channel.send ( 'receive' )
  28. channel.close()
  29. break
  30. else:
  31. print "close" , details
  32. channel.send ( 'connection closed' )
  33. channel.close()
  34. conn_active = False
  35. start_server = False

Report this snippet 

You need to login to post a comment.