Navigation

    Odoo 中文社区

    • Register
    • Login
    • Search
    • Categories
    • Tags
    • Popular
    • Users
    • Groups

    Odoo中文社区可以通过以下两个域名访问:shine-it.net , odoo.net.cn

    由于系统升迁的原因,本论坛部分较早期的内容存在格式和链接损坏失效的问题,并非本论坛系统本身的缺陷,望谅解

    本社区没有维护任何QQ群讨论组,任何与本社区同名的QQ群讨论组的言论与本社区无关!

    开发人员可以登录gitter讨论组: http://gitter.im/odoo-china/Talk, 需要github账号

    如果您登录系统碰到问题,请在微信公众号留言:

    分享一个自己编写的脚本 进程监听端口列表 Windows平台

    Python 开发
    3
    13
    27918
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • 暗
      暗香 last edited by

      分享一个自己编写的脚本,用于查看进程都监听了哪些端口。
      代码如下:

      from subprocess import Popen as popen<br />import re<br />import os<br /><br />fname = &#039;tmp.txt&#039;<br />fout = open(fname, &#039;wb&#039;)<br />p = popen(&#039;netstat -nao&#039;, stdout=fout)<br />p.wait()<br />p = popen(&#039;tasklist&#039;, stdout=fout)<br />p.wait()<br />fout.close()<br /><br />fin = open(fname, &#039;rb&#039;)<br />buf = fin.read()<br />fin.close()<br />os.remove(fname)<br /><br />p1 = &#039;:(\d{1,5}) +?.+?LISTENING +?(\d{1,5})&#039;<br />p2 = &#039;(.+?)&nbsp; +?(\d{1,5}) Console|(.+?)&nbsp; +?(\d{1,5}) Services&#039;<br /><br />ports = {}<br />for i in re.finditer(p1, buf):<br />&nbsp; &nbsp; _port = i.group(1)<br />&nbsp; &nbsp; _pid = i.group(2)<br />&nbsp; &nbsp; if not ports.has_key(_pid):<br />&nbsp; &nbsp; &nbsp; &nbsp; ports[_pid] = [_port]<br />&nbsp; &nbsp; else:<br />&nbsp; &nbsp; &nbsp; &nbsp; ports[_pid].append(_port)<br />names = {}<br />c = 0<br />for i in re.finditer(p2, buf):<br />&nbsp; &nbsp; _name = i.group(1)<br />&nbsp; &nbsp; _pid = i.group(2)<br />&nbsp; &nbsp; if _name:<br />&nbsp; &nbsp; &nbsp; &nbsp; names[_pid] = _name<br />&nbsp; &nbsp; else:<br />&nbsp; &nbsp; &nbsp; &nbsp; _name = i.group(3)<br />&nbsp; &nbsp; &nbsp; &nbsp; _pid = i.group(4)<br />&nbsp; &nbsp; &nbsp; &nbsp; names[_pid] = _name<br /><br />print &#039;-&#039; * 79<br />for d in names:<br />&nbsp; &nbsp; if ports.has_key(d):<br />&nbsp; &nbsp; &nbsp; &nbsp; print &#039;Process&#039;, names[d], &#039;(pid %s)&#039; % d, &#039;ports:&#039;, &#039;, &#039;.join(ports[d])<br />print &#039;-&#039; * 79<br />raw_input(&#039;Press ENTER key to continue ...&#039;)<br />
      



      使用管道,替换掉前边创建文件,读写文件的过程。

      buf = &#039;&#039;<br />p = popen(&#039;netstat -nao&#039;, bufsize=1024, stdout=pipe)<br />buf += p.stdout.read()<br />p = popen(&#039;tasklist&#039;, bufsize=1024, stdout=pipe)<br />buf += p.stdout.read()
      



      感谢[b]mrshelly[/b]的提示。

      1 Reply Last reply Reply Quote 0
      • 暗
        暗香 last edited by

        主要的思路是综合Windows下的两个命令“netstat”和“tasklist”的输出,分析出本机上的进程都监听了哪些端口。

        1 Reply Last reply Reply Quote 0
        • 暗
          暗香 last edited by

          原打算使用StringIO对象,但是popen报错,说StringIO对象没有fileno属性,只能替换成文件对象。不知道各位有没有可以替代的方法,使用内存,不用在磁盘上生成临时文件。

          1 Reply Last reply Reply Quote 0
          • mrshelly
            mrshelly last edited by

            不错支持一下....

            Tags 是用英文","分隔的...
            另:  请参考:<br / http://docs.python.org/library/tempfile.html br />&<br / [检测到链接无效,已移除] br />

            1 Reply Last reply Reply Quote 0
            • 暗
              暗香 last edited by

              谢谢。有没有办法把popen出的进程输出重定向到内存,而不用在磁盘建立临时文件?

              1 Reply Last reply Reply Quote 0
              • mrshelly
                mrshelly last edited by

                <br />...<br />&gt;&gt;&gt; pop = subprocess.Popen(&#039;cmd /c dir&#039;, stdin=subprocess.PIPE, stdout=subproces<br />s.PIPE, close_fds=False)<br />&gt;&gt;&gt; print pop.stdout.read()<br />...<br />
                
                1 Reply Last reply Reply Quote 0
                • digitalsatori
                  digitalsatori 管理员 last edited by

                  试试 http://code.google.com/p/psutil/ 模块,你可以更优雅的实现你要的功能,而且更重要的是它是夸平台的。

                  1 Reply Last reply Reply Quote 0
                  • 暗
                    暗香 last edited by

                    [quote author=mrshelly link=topic=2704.msg9101#msg9101 date=1324369221]

                    <br />...<br />&gt;&gt;&gt; pop = subprocess.Popen(&#039;cmd /c dir&#039;, stdin=subprocess.PIPE, stdout=subproces<br />s.PIPE, close_fds=False)<br />&gt;&gt;&gt; print pop.stdout.read()<br />...<br />
                    


                    [/quote]

                    谢谢你的提示,通过使用管道很好的处理了这个问题。

                    1 Reply Last reply Reply Quote 0
                    • 暗
                      暗香 last edited by

                      [quote author=digitalsatori link=topic=2704.msg9102#msg9102 date=1324369661]
                      试试 http://code.google.com/p/psutil/ 模块,你可以更优雅的实现你要的功能,而且更重要的是它是夸平台的。
                      [/quote]

                      谢谢你的建议,看了psutil包的介绍,功能很强大。在系统管理上会很有用。

                      1 Reply Last reply Reply Quote 0
                      • mrshelly
                        mrshelly last edited by

                        psutil 官方没有放出 编译版本 相当麻烦啊...

                        自己编译也很麻烦...

                        1 Reply Last reply Reply Quote 0
                        • 暗
                          暗香 last edited by

                          [quote author=mrshelly link=topic=2704.msg9105#msg9105 date=1324437122]
                          psutil 官方没有放出 编译版本 相当麻烦啊...

                          自己编译也很麻烦...
                          [/quote]

                          现在已放出了编译版本,首页上有2.7和3.2的下载链接。

                          1 Reply Last reply Reply Quote 0
                          • mrshelly
                            mrshelly last edited by

                            看到了. 也有2.4,2.5的下载链接.

                            晚上试了一下. 是个蛮强大的扩展... 很棒..... 看来可以用OE做一些服务器管理的模块了....

                            1 Reply Last reply Reply Quote 0
                            • First post
                              Last post