跳转至内容
  • 版块
  • 标签
  • 热门
  • 用户
  • 群组
皮肤
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • 默认(Flatly)
  • 不使用皮肤
折叠

Odoo 中文社区

暗

暗香

@暗香
关于
帖子
8
主题
2
群组
0
粉丝
0
关注
0

帖子

最新 最佳 有争议的

  • 演示服务器现在不能用了吗?
    暗 暗香

    刚刚访问演示服务器,可是火狐提示“Unable to connect”错误,这是肿么回事呢?


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

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

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

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


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

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

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


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

    [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]

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


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

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


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

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


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

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


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

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

    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]的提示。

  • 登录

  • 没有帐号? 注册

  • 登录或注册以进行搜索。
  • 第一个帖子
    最后一个帖子
0
  • 版块
  • 标签
  • 热门
  • 用户
  • 群组