Created
June 25, 2011 04:32
-
-
Save seungjin/1046137 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ python port_checker_tcp.py -a 192.168.1.15 -p 80 | |
| $ python port_checker_tcp.py -a 192.168.1.15 -p 80 && echo "SUCCESS" | |
| $ python port_checker_tcp.py -a 192.168.1.15 -p 81 && echo "FAILURE" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## {{{ http://code.activestate.com/recipes/577769/ (r1) | |
| #!/usr/bin/env python | |
| # -*- coding: utf-8 -*- | |
| # Originally from http://code.activestate.com/recipes/577769-tcp-port-checker/ | |
| import socket | |
| import re | |
| import sys | |
| def check_server(address, port): | |
| # Create a TCP socket | |
| s = socket.socket() | |
| print "Attempting to connect to %s on port %s" % (address, port) | |
| try: | |
| s.connect((address, port)) | |
| print "Connected to %s on port %s" % (address, port) | |
| return True | |
| except socket.error, e: | |
| print "Connection to %s on port %s failed: %s" % (address, port, e) | |
| return False | |
| if __name__ == '__main__': | |
| from optparse import OptionParser | |
| parser = OptionParser() | |
| parser.add_option("-a", "--address", dest="address", default='localhost', help="ADDRESS for server", metavar="ADDRESS") | |
| parser.add_option("-p", "--port", dest="port", type="int", default=80, help="PORT for server", metavar="PORT") | |
| (options, args) = parser.parse_args() | |
| print 'options: %s, args: %s' % (options, args) | |
| check = check_server(options.address, options.port) | |
| print 'check_server returned %s' % check | |
| sys.exit(not check) | |
| ## end of http://code.activestate.com/recipes/577769/ }}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment