Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # Copyright (C) 2014 SISTEMAS O.R.P.
- #
- # This program is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License, or
- # (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
- import select
- import socket
- import errno
- port = 50000
- client1 = None
- client2 = None
- loop = 1
- print "Arduino TCP/IP programming tunnel"
- server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- server.bind(('',port))
- server.listen(2)
- inputs = [server]
- print "Waiting for both connections..."
- while loop:
- rlist, wlist, xlist = select.select(inputs,[],[])
- for s in rlist:
- if s == server:
- if client1 is None:
- client1, address1 = s.accept()
- inputs.append(client1)
- print address1[0], " (1) connected"
- elif client2 is None:
- client2, address2 = s.accept()
- inputs.append(client2)
- print address2[0], " (2) connected"
- else:
- client3, address3 = s.accept()
- client3.send("Get out of here!!!")
- client3.close()
- print "An unexpected connection from", address3[0], "has been closed."
- else:
- disconnection = 0
- try:
- data = s.recv(1024)
- except socket.error as error:
- if error.errno == errno.WSAECONNRESET:
- disconnection = 1
- else:
- raise
- if disconnection == 0 and data:
- if s == client1:
- if client2 is not None:
- client2.send(data)
- else:
- if client1 is not None:
- client1.send(data)
- else:
- if s == client1:
- print address1[0]," (1) disconnected"
- inputs.remove(client1)
- if client2 is not None:
- inputs.remove(client2)
- client2.close()
- else:
- print address2[0]," (2) disconnected"
- inputs.remove(client2)
- if client1 is not None:
- inputs.remove(client1)
- client1.close()
- client1 = None
- client2 = None
- loop = 0
- server.close()
- print "Programming tunnel finished"
Add Comment
Please, Sign In to add comment