#!/usr/bin/python\n\nimport sys, socket\n\nif (len(sys.argv) < 2):\n sys.exit('Must supply port!\\n')\n\nport = sys.argv[1]\nhost = socket.gethostbyname(socket.gethostname())\n\nprint '%s:%s Listening:\\n' % (host, port)\n\nsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\ntry:\n sock.bind((host, int(port)))\n sock.listen(1)\n\n conn, addr = sock.accept()\n print 'Connection from: ' + str(addr)\n\n str = conn.recv(1024)\n print 'Echoing "%s"' % (str)\n conn.send(str)\n\n conn.close()\n\nexcept socket.error, (errno, errstr):\n conn.close()\n sys.exit("Error: %s!" % (errstr))
Client:
#!/usr/bin/python\n\nimport sys, socket\n\nif (len(sys.argv) < 4):\n sys.exit('Must supply host, port, and text string to send!\\n')\n\n(host, port, string) = sys.argv[1:]\n\nprint '%s:%s Sending: %s\\n' % (host, port, string)\n\nsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\ntry:\n sock.connect((host, int(port)))\n sock.sendall('%s\\n' % (string))\n recv = ''\n while 1:\n str = sock.recv(1024)\n if not str: break\n recv += str\n\nexcept socket.error, (errno, errstr):\n sock.close()\n sys.exit("Error: %s!" % (errstr))\n\nprint "Received:\\n" + recv