V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
dbow
V2EX  ›  Python

分享查找绑定到某个端口的进程的一段代码。

  •  
  •   dbow ·
    maliubiao · 2014-09-05 20:39:17 +08:00 · 3522 次点击
    这是一个创建于 3537 天前的主题,其中的信息可能已经有所发展或是发生改变。
    gist: url://gist.github.com/maliubiao/831c804058caddbedc15
    功能介绍
    python tcpinfo.py -p port -> 相应的进程
    比如本机的端口47313
    输出为
    pid: 9567
    exe: /usr/lib64/firefox/firefox
    cwd: /home/user
    cmdline: /usr/lib64/firefox/firefox

    python tcpinfo.py -t是比较快速的了解tcp链接情况的方法,比netstat快一些。

    可以直接import tcpinfo作为模块用, 不必shell调用然后解析netstat的结果, 这样更快, 代码简单易懂, 欢迎反馈。
    4 条回复    2014-09-11 13:43:07 +08:00
    est
        1
    est  
       2014-09-05 22:33:32 +08:00
    赞一个。虽然 /proc/net/tcp 不是效率最高的。
    yingluck
        2
    yingluck  
       2014-09-05 22:50:48 +08:00
    lsof -i:port 是这个的功能?
    dbow
        3
    dbow  
    OP
       2014-09-05 22:59:00 +08:00
    @yingluck 同一个原理, 都是根据/proc/net/tcp里的链接对应的inode id, 然后遍历所有进程的fd对应inode id, 我这个版本只考虑绑定占用这个端口的进程(就一个结果), 不考虑连接这个端口的进程
    hustluo
        4
    hustluo  
       2014-09-11 13:43:07 +08:00
    您好,我是罗力铭华中科技大学。现在在做中小学在线教育,立志于打造一款中小学知识点视频词典,学生不懂的题,我这里都有讲解。我们的网站和APP第一版已经开发完成,www.tuomeng.com.cn 现在招募Python的技术合伙人和在线教育的运营合伙人,身边有合适的人还望推荐,或者自荐,谢谢。昨天一天一个人发了个帖子,400个初二以上的中学生加我QQ群,现在也有几十万的用户信息,我QQ360714168,希望能够详聊
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3475 人在线   最高记录 6547   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 12:05 · PVG 20:05 · LAX 05:05 · JFK 08:05
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.