参考:https://blog.51cto.com/jaysonzhang/1200405
#!/usr/bin/python
# -*- coding: UTF-8 -*-
from BaseHTTPServer import *
import socket
import re
class Handler(BaseHTTPRequestHandler):
def process(self):
try:
ip = self.path.split('?')[1].split('&')[0].split('=')[1]
port = self.path.split('?')[1].split('&')[1].split('=')[1]
if (re.match(r"^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$",ip)) and (re.match(r"^(6[0-5]{2}[0-3][0-5]|[1-5]\d{4}|[1-9]\d{1,3}|[0-9])$",port)):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(3)
success = str( ip+":"+port+ " is opend\n")
s.connect((ip, int(port)))
s.shutdown(socket.SHUT_RDWR)
return success
else:
fail=str( ip+":"+port+ " IP或端口输入不合法\n")
return fail
except:
fail=str( ip+":"+port+ " is closed\n")
return fail
def do_GET(self):
self.send_response(200)
buf = self.process()
self.send_header("Content-Type", "text/plain; charset=utf8")
self.send_header("Content-Length", str(len(buf)))
self.end_headers()
self.wfile.write(buf)
httpd = HTTPServer(("", 10000), Handler)
print "Server starting ..."
try:
httpd.serve_forever()
except KeyboardInterrupt: exit()
[root@centos7 ~]# curl 'http://192.168.11.200:10000/?ip=0&port=53'
0:53 IP或端口输入不合法
[root@centos7 ~]# curl 'http://192.168.11.200:10000/?ip=1.1.1.1&port=53'
1.1.1.1:53 is closed
[root@centos7 ~]# curl 'http://192.168.11.200:10000/?ip=8.8.8.8&port=53'
8.8.8.8:53 is opend