推荐学习书目
› 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 · Sep 5, 2014 · 4356 views
    This topic created in 4404 days ago, the information mentioned may be changed or developed.
    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 replies  •  2014-09-11 13:43:07 +08:00
    est
        1
    est  
       Sep 5, 2014
    赞一个。虽然 /proc/net/tcp 不是效率最高的。
    yingluck
        2
    yingluck  
       Sep 5, 2014
    lsof -i:port 是这个的功能?
    dbow
        3
    dbow  
    OP
       Sep 5, 2014
    @yingluck 同一个原理, 都是根据/proc/net/tcp里的链接对应的inode id, 然后遍历所有进程的fd对应inode id, 我这个版本只考虑绑定占用这个端口的进程(就一个结果), 不考虑连接这个端口的进程
    hustluo
        4
    hustluo  
       Sep 11, 2014
    您好,我是罗力铭华中科技大学。现在在做中小学在线教育,立志于打造一款中小学知识点视频词典,学生不懂的题,我这里都有讲解。我们的网站和APP第一版已经开发完成,www.tuomeng.com.cn 现在招募Python的技术合伙人和在线教育的运营合伙人,身边有合适的人还望推荐,或者自荐,谢谢。昨天一天一个人发了个帖子,400个初二以上的中学生加我QQ群,现在也有几十万的用户信息,我QQ360714168,希望能够详聊
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2734 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 13:31 · PVG 21:31 · LAX 06:31 · JFK 09:31
    ♥ Do have faith in what you're doing.