Commits

Anonymous committed 8035fbd

Added routine to test a TCP connection (since I can no longer use telnet in Windows 7 and Console 2 without problems)

Comments (0)

Files changed (2)

jaraco/net/tcp.py

+import socket
+from optparse import OptionParser
+
+def get_connect_options():
+	parser = OptionParser(conflict_handler="resolve")
+	parser.add_option('-h', '--host', default='localhost')
+	parser.add_option('-p', '--port', default=80, type='int')
+	options, args = parser.parse_args()
+	if not len(args) == 0:
+		parser.error("Unexpected positional argument")
+	return options
+
+def test_connect():
+	options = get_connect_options()
+	addr = options.host, options.port
+	family, socktype, proto, canonname, sockaddr = socket.getaddrinfo(*addr)[0]
+	sock = socket.socket(family, socktype, proto)
+	try:
+		conn = sock.connect(sockaddr)
+	except socket.error as e:
+		print e
+		raise SystemExit(1)
+	args = vars(options)
+	print "Successfully connected to {host} on port {port}".format(**args)
+	
 			'dnsbl-check = jaraco.net.dnsbl:handle_cmdline',
 			'ntp = jaraco.net.ntp:handle_command_line',
 			'remove-known-spammers = jaraco.net.email:remove_known_spammers',
+			'tcp-test-connect = jaraco.net.tcp:test_connect',
 			],
 	},
 	install_requires=[