Guest User

Arduino TCP/IP programming tunnel by SISTEMAS O.R.P.

a guest
Oct 28th, 2014
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.40 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # Copyright (C) 2014 SISTEMAS O.R.P.
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17.  
  18. import select
  19. import socket
  20. import errno
  21.  
  22. port = 50000
  23. client1 = None
  24. client2 = None
  25. loop = 1
  26.  
  27. print "Arduino TCP/IP programming tunnel"
  28. server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  29. server.bind(('',port))
  30. server.listen(2)
  31. inputs = [server]
  32.  
  33. print "Waiting for both connections..."
  34.  
  35. while loop:
  36.     rlist, wlist, xlist = select.select(inputs,[],[])
  37.  
  38.     for s in rlist:
  39.  
  40.         if s == server:
  41.             if client1 is None:
  42.                 client1, address1 = s.accept()
  43.                 inputs.append(client1)
  44.                 print address1[0], " (1) connected"
  45.             elif client2 is None:
  46.                 client2, address2 = s.accept()
  47.                 inputs.append(client2)
  48.                 print address2[0], " (2) connected"
  49.             else:
  50.                 client3, address3 = s.accept()
  51.                 client3.send("Get out of here!!!")
  52.                 client3.close()
  53.                 print "An unexpected connection from", address3[0], "has been closed."
  54.                
  55.         else:
  56.             disconnection = 0
  57.             try:
  58.                 data = s.recv(1024)
  59.             except socket.error as error:
  60.                 if error.errno == errno.WSAECONNRESET:
  61.                     disconnection = 1
  62.                 else:
  63.                     raise
  64.            
  65.             if disconnection == 0 and data:
  66.                 if s == client1:
  67.                     if client2 is not None:
  68.                         client2.send(data)
  69.                 else:
  70.                     if client1 is not None:
  71.                         client1.send(data)
  72.             else:
  73.                 if s == client1:
  74.                     print address1[0]," (1) disconnected"
  75.                     inputs.remove(client1)
  76.                     if client2 is not None:
  77.                         inputs.remove(client2)
  78.                         client2.close()
  79.                 else:
  80.                     print address2[0]," (2) disconnected"
  81.                     inputs.remove(client2)
  82.                     if client1 is not None:
  83.                         inputs.remove(client1)
  84.                         client1.close()
  85.                        
  86.                
  87.                 client1 = None
  88.                 client2 = None             
  89.                 loop = 0
  90. server.close()
  91. print "Programming tunnel finished"
Add Comment
Please, Sign In to add comment