PageRenderTime 54ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/python-socket-server.py

https://github.com/drewlesueur/python-socket-server
Python | 118 lines | 82 code | 17 blank | 19 comment | 3 complexity | 1556c5631f4a54c27aec5a71b53e085c MD5 | raw file
  1. import socket
  2. import select
  3. import time
  4. #http://forums.devshed.com/python-programming-11/a-few-socket-questions-75012.html
  5. #http://davidf.sjsoft.com/mirrors/mcmillan-inc/sock1.html
  6. #try assyncore
  7. #create an INET, STREAMing socket
  8. serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  9. #bind the socket to a public host,
  10. # and a well-known port
  11. port = 5555
  12. serversocket.bind((socket.gethostname(), port)) #this is the one you want
  13. #serversocket.bind(("127.0.0.1", port))
  14. """
  15. to connect if you have a router
  16. type ipconfig in dos and get your routers ip address
  17. eg: 192.168.1.105
  18. """
  19. #become a server socket
  20. serversocket.listen(5)
  21. serversocket.setblocking(0)
  22. #serversocket.settimeout(10000);
  23. #serversocket.setblocking(0)
  24. #serversocket.settimeout(.01);
  25. def handle_data(data):
  26. if data[0:len("<policy-file-request/>")] == "<policy-file-request/>":
  27. print socko.send('<?xml version=\"1.0\"?><cross-domain-policy><allow-access-from domain="*" to-ports= "*" /></cross-domain-policy>')
  28. print "sent policy file"
  29. closesocket(socko)
  30. else:
  31. print "you said" + data
  32. connections = []
  33. conn_info = [];
  34. counter = 0;
  35. print "going to listen on port " + str(port)
  36. def closesocket(socko):
  37. socko.close()
  38. connections.remove(socko) #remove is pretty cool if it works!
  39. socko = None
  40. while 1:
  41. """counter = counter + 1
  42. if counter == 1000:
  43. counter = 0
  44. print "reset"
  45. for y in range(0,len(connections)):
  46. print conn_info[y] """
  47. time.sleep(.001) #change this in the future for speed
  48. ready_to_read, ready_to_write, in_error = select.select([serversocket],[],[],0) #should I change that 0 to .001
  49. #print len(ready_to_read)
  50. for sock in ready_to_read:
  51. print "test"
  52. (clientsocket, address) = sock.accept()
  53. #print clientsocket
  54. #print address
  55. print "putting " + address[0] + " onto connections";
  56. conn_info.append(address)
  57. clientsocket.setblocking(0)
  58. connections.append(clientsocket)
  59. print len(connections)
  60. if len(connections) > 0:
  61. to_read, to_write, to_error = select.select(connections,connections,connections,0)
  62. for socko in to_read:
  63. #i needed this try block only for google chrome! wierd.
  64. try:
  65. data = socko.recv(1024) #1024 potential problem causer
  66. except socket.error, msg:
  67. closesocket(socko)
  68. break
  69. print data
  70. if not data:
  71. print "no data"
  72. closesocket(socko)
  73. break
  74. try:
  75. #print "this is the first" + data[0]
  76. #for flash policy stuff
  77. #socko.send(data)
  78. for sockwrite in to_write:
  79. print "test"
  80. ret = handle_data(data)
  81. if ret != None:
  82. sockwrite.send(ret)
  83. #print socko.send("HTTP/1.0 200 OK\r\n")
  84. #print socko.send("Content-Type: text/html\r\n")
  85. #socko.send("Content-Length: 16\r\n\r\nThis is the text")
  86. print "i sent output"
  87. except socket.error, msg:
  88. closesocket(socko)
  89. for socko in to_error:
  90. print socko + " had an error"