Tuesday, March 8, 2011

[Level 2] How to detect port with python script.

If you want to detect port, you can use socket module to do it.

#!/bin/env python
import os, sys
import socket

def detectPort(PORT):
    # Echo server program
    import socket
    HOST = ''
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        s.bind((HOST, PORT))
        return_code=1
    except:
        return_code=0
    s.close()
    return return_code

if __name__ == '__main__':
  for PORT in [80,81,82,8000,8080,3306,3307]:
    if detectPort(PORT): print PORT




Wish this helps.
regards,
Stanley Huang