zoukankan      html  css  js  c++  java
  • HTTP监视网络

    import BaseHTTPServer, shutil, os
    from cStringIO import StringIO
    class MyHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
        #The http path we service and the command we service
        cmds = {'/ping':'ping www.thinkware.se',
                '/netstat': 'netstat -a',
                '/tracert': 'tracert www.thinkware.se',
                '/srvstats': 'net statistics server',
                '/wsstats': 'net statistics workstation',
                '/route': 'route print',
                }
    
        def do_GET(self):
            """service a GET request"""
            f = self.send_head()
            if f:
                f = StringIO()
                machine = os.popen('hostname').readlines()[0]
                if self.path == '/':
                    heading = "Select a command to run on %s" %(machine)
                    body = ( self.getMenu() + "<p> The screen won't update until the selected" "command has finished. Please be patient.")
                else:
                    heading = "Execution of ''%s'' on %s" %( self.cmds[self.path], machine)
                    cmd = self.cmds[self.path]
                    body = '<a href="/">Main Menu&lt;/a&gt;<pre>%s</pre>\n' %  os.popen(cmd).read()
                    body = body.decode('cp437').encode('latin1')
                f.write("<html><head><title>%s</title></head>\n" %heading)
                f.write('<body><H1>%s</H1>\n' %(heading))
                f.write(body)
                f.write('</body></html>\n')
                f.seek(0)
                self.copyfile(f, self.wfile)
                f.close()
    
            return f
        
        def do_HEAD(self):
            """service a head request"""
            f = self.send_head()
            if f:
                f.close()
    
        def send_head(self):
            path = self.path
            if not path in ['/'] + self.cmds.keys():
                head = 'Command "%s" not found. Try one of three:<ul>' %path
                msg = head + self.getMenu()
                self.send_error(404, msg)
                return None
            self.send_response(200)
            self.send_header("Content-type", 'text/html')
            self.end_headers()
            f = StringIO()
            f.write("A test %s \n" % self.path)
            f.seek(0)
            return f
    
        def getMenu(self):
            keys = self.cmds.keys()
            keys.sort()
            msg = []
            for k in keys:
                msg.append('<li><a href = "%s"> %s => %s&lt;/a&gt;</li>' %(k, k, self.cmds[k]))
            msg.append('</ul>')
            return "\n".join(msg)
    
        def copyfile(self, source, outputfile):
            shutil.copyfileobj(source, outputfile)
    
    def main(HandleClass = MyHTTPRequestHandler, ServerClass = BaseHTTPServer.HTTPServer):
        BaseHTTPServer.test(HandleClass, ServerClass)
    
    if __name__ == '__main__':
        main()

    运行输出:

    Serving HTTP on 0.0.0.0 port 8000 ...
    localhost.localdomain - - [25/Aug/2012 01:14:18] "GET / HTTP/1.1" 200 -
    localhost.localdomain - - [25/Aug/2012 01:14:18] code 404, message Command "/favicon.ico" not found. Try one of three:<ul><li><a href = "/netstat"> /netstat => netstat -a&lt;/a&gt;</li>
    <li><a href = "/ping"> /ping => ping www.thinkware.se&lt;/a&gt;</li>
    <li><a href = "/route"> /route => route print&lt;/a&gt;</li>
    <li><a href = "/srvstats"> /srvstats => net statistics server&lt;/a&gt;</li>
    <li><a href = "/tracert"> /tracert => tracert www.thinkware.se&lt;/a&gt;</li>
    <li><a href = "/wsstats"> /wsstats => net statistics workstation&lt;/a&gt;</li>
    </ul>
    localhost.localdomain - - [25/Aug/2012 01:14:18] "GET /favicon.ico HTTP/1.1" 404 -
    localhost.localdomain - - [25/Aug/2012 01:14:20] "GET /netstat HTTP/1.1" 200 -
    localhost.localdomain - - [25/Aug/2012 01:14:20] code 404, message Command "/favicon.ico" not found. Try one of three:<ul><li><a href = "/netstat"> /netstat => netstat -a&lt;/a&gt;</li>
    <li><a href = "/ping"> /ping => ping www.thinkware.se&lt;/a&gt;</li>
    <li><a href = "/route"> /route => route print&lt;/a&gt;</li>
    <li><a href = "/srvstats"> /srvstats => net statistics server&lt;/a&gt;</li>
    <li><a href = "/tracert"> /tracert => tracert www.thinkware.se&lt;/a&gt;</li>
    <li><a href = "/wsstats"> /wsstats => net statistics workstation&lt;/a&gt;</li>
    </ul>
    localhost.localdomain - - [25/Aug/2012 01:14:20] "GET /favicon.ico HTTP/1.1" 404 -
    localhost.localdomain - - [25/Aug/2012 01:14:32] code 404, message Command "/favicon.ico" not found. Try one of three:<ul><li><a href = "/netstat"> /netstat => netstat -a&lt;/a&gt;</li>
    <li><a href = "/ping"> /ping => ping www.thinkware.se&lt;/a&gt;</li>
    <li><a href = "/route"> /route => route print&lt;/a&gt;</li>
    <li><a href = "/srvstats"> /srvstats => net statistics server&lt;/a&gt;</li>
    <li><a href = "/tracert"> /tracert => tracert www.thinkware.se&lt;/a&gt;</li>
    <li><a href = "/wsstats"> /wsstats => net statistics workstation&lt;/a&gt;</li>
    </ul>
    localhost.localdomain - - [25/Aug/2012 01:14:32] "GET /favicon.ico HTTP/1.1" 404 -
    localhost.localdomain - - [25/Aug/2012 01:14:34] "GET /ping HTTP/1.1" 200 -

  • 相关阅读:
    API函数
    平台调用教程
    查看网页源文件方法
    网页端商品链接转换为手机端链接的部分网址规则
    中文分词消除歧义简单思想
    java 链接数据库时的配置代码
    手机参数更新语句根据Id 可以得到某手机的各种参数
    中文分词—基于Lucene的分词器—支持中英文混合词
    修改Imdict做自己的分词器
    制作可输入的下拉框
  • 原文地址:https://www.cnblogs.com/hzhida/p/2655592.html
Copyright © 2011-2022 走看看