Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Untitled

a guest Nov 16th, 2018 86 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. import socket
  2. from time import sleep
  3. import traceback
  4. import serial
  5. import argparse
  6.  
  7. USB_DEVICE = '/dev/cu.usbmodem3580231'
  8. PACKET_SIZE = 256
  9.  
  10. parser = argparse.ArgumentParser(description='Connect')
  11. parser.add_argument("ip", help="other device ip")
  12. parser.add_argument("port", help="Port to listen and write on other device")
  13. parser.add_argument('-c', dest='caller', action='store_true',
  14.                     help='if true this script is "calling"')
  15.  
  16.  
  17. def wait_for_connection(port):
  18.     in_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  19.     in_sock.bind(('localhost', int(port)))
  20.     in_sock.listen(1)
  21.     return in_sock.accept()[0]
  22.  
  23.  
  24. def connect(ip, port):
  25.     while True:
  26.         try:
  27.             out = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  28.             out.connect((ip, int(port)))
  29.             print("Connected")
  30.             return out
  31.         except:
  32.             traceback.print_exc()
  33.             print("Cannot establish connection to", ip, port)
  34.             sleep(1)
  35.  
  36.  
  37. if __name__ == "__main__":
  38.     args = parser.parse_args()
  39.     caller = args.caller
  40.  
  41.     if caller:
  42.         socket = wait_for_connection(args.port)
  43.     else:
  44.         socket = connect(args.ip, args.port)
  45.  
  46.     #ser = serial.Serial(USB_DEVICE)
  47.  
  48.     while True:
  49.         if not caller:
  50.             socket.send(b"TEST")
  51.             value = socket.recv(255)
  52.             print(value)
  53.         else:
  54.             socket.send(socket.recv(255))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top