Servidor de echo


/ Published in: Python
Save to your folder(s)



Copy this code and paste it in your HTML
  1. import socket
  2. import sys
  3.  
  4. host = ''
  5. port = 50000
  6. backlog = 5
  7. size = 1024
  8. s = None
  9. try:
  10. s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
  11. s.bind((host,port))
  12. s.listen(backlog)
  13. except socket.error,(value,message):
  14. if s:
  15. s.close()
  16. print "Could not open socket: " + message
  17. sys.exit(1)
  18. while 1:
  19. client,address = s.accept()
  20. data = client.recv(size)
  21. if data:
  22. print(data)
  23. client.send(data)
  24. if (data=="quit"):
  25. sys.exit(0)
  26. client.close()

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.