V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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
ansheng
V2EX  ›  Python

怎么理解 Python 中的 socket 工作原理?

  •  1
     
  •   ansheng ·
    anshengme · Oct 20, 2016 · 4025 views
    This topic created in 3475 days ago, the information mentioned may be changed or developed.

    最近在整网络这块的东西,恰好弄到 socket 了,感觉理解的好笼统,一点那都不专业咋办?各位 V 友有没有对应文章推荐呢?

    31 replies    2016-10-21 09:12:39 +08:00
    julyclyde
        1
    julyclyde  
       Oct 20, 2016
    你问的是 socket
    还是 python 里的 socket
    还是 python
    ansheng
        2
    ansheng  
    OP
       Oct 20, 2016
    @julyclyde python 里的 socket
    BOYPT
        3
    BOYPT  
       Oct 20, 2016   ❤️ 1
    python 易用,但是不代表你不用学操作系统的东西,找本网络编程补补课吧。
    julyclyde
        4
    julyclyde  
       Oct 20, 2016
    @ansheng python 里的 socket 就是操作系统里的 socket 直接包了一层而已
    ansheng
        5
    ansheng  
    OP
       Oct 20, 2016
    @julyclyde 那我是否可以理解他其实就在服务器上开了一扇门,然后客户端通过找到这扇门进行通讯,建立的连接是通过 tcp/udp 协力的,这其中是否牵扯到了 http 呢?
    introom
        6
    introom  
       Oct 20, 2016   ❤️ 6
    ansheng
        7
    ansheng  
    OP
       Oct 20, 2016
    scream7
        8
    scream7  
    PRO
       Oct 20, 2016
    拿 python 学 socket 的话强烈推荐使用 ipython ,甚至可以帮你把 tcp 的各个状态理解清楚
    ansheng
        9
    ansheng  
    OP
       Oct 20, 2016
    @scream7 我只想知道他的流程。。。。
    9hills
        10
    9hills  
       Oct 20, 2016   ❤️ 1
    Python 的 Socket 和 C 的 Socket 没啥区别,简单包了一层

    建议先学习一本书叫《 Unix 高级环境编程》中的 socket 通信部分章节,这本书是所有*NIX 环境下,不管你用什么语言,都必须先学习的一本书。。。
    zhangbohun
        11
    zhangbohun  
       Oct 20, 2016 via Android
    楼主需要补充一下计算机网络相关的姿势
    lfzyx
        12
    lfzyx  
       Oct 20, 2016
    python 中的 socket 只是用 python 实现的而已,跟其他语言的 socket 工作原理并没有什么不同
    cxl008
        13
    cxl008  
       Oct 20, 2016
    这和 python 有什么关系 ,是你自己不理解 socket 。。。
    zhicheng
        14
    zhicheng  
       Oct 20, 2016   ❤️ 2
    不知道你说的是 socket 还是网络编程,下面两篇网络编程的文章推荐给你看一下。

    用 Python 理解服务器模型(上) https://www.textarea.com/zhicheng/yong-python-lijie-fuwuqi-moxing-shang-566/
    用 Python 理解服务器模型(下) https://www.textarea.com/zhicheng/yong-python-lijie-fuwuqi-moxing-xia-596/
    ansheng
        15
    ansheng  
    OP
       Oct 20, 2016
    @zhicheng socket
    zhicheng
        16
    zhicheng  
       Oct 20, 2016
    @ansheng socket 就简单了,看我发的文章里第一章就够了。
    meinqy
        17
    meinqy  
       Oct 20, 2016
    你可以这样理解: Python 通过调用系统底层 叫 socket 的 api 实现 和 客户机 的 tcp/udp 通信
    ansheng
        18
    ansheng  
    OP
       Oct 20, 2016
    ansheng
        19
    ansheng  
    OP
       Oct 20, 2016
    @meinqy socket 其实就是对 tcp/ip 、 tcp/udp 的一个封装,也就是 python 提供了一个的网络打交道的模块把。
    imn1
        20
    imn1  
       Oct 20, 2016
    根据 5L 的回复,你先需要学习了解 BSD socket 原理, python 跟它是基本一样的,连 error no 都一样
    注意同一版的 python ,在 linux 和 windows 上 socket 封装是不同的,返回状态和 error no 是跟随系统,如上面所说只是打包
    R4rvZ6agNVWr56V0
        21
    R4rvZ6agNVWr56V0  
       Oct 20, 2016
    可以看《 Python 网络编程》 这本书
    ansheng
        22
    ansheng  
    OP
       Oct 20, 2016
    @GeekGao 不是 UNIX 网络编程?
    meinqy
        23
    meinqy  
       Oct 20, 2016
    @ansheng ip 协议网络层的, tcp/udp 协议是传输层的 不是一个层级的东西。 如果你只是想大概明白 socket 基本就我说的意思哦~~
    R4rvZ6agNVWr56V0
        24
    R4rvZ6agNVWr56V0  
       Oct 20, 2016
    @GeekGao 先会用,再去分析原理才是正路
    ansheng
        25
    ansheng  
    OP
       Oct 20, 2016
    @GeekGao 会用了,只是想了解下原理方面,毕竟这个东西应用还是蛮多的。
    R4rvZ6agNVWr56V0
        26
    R4rvZ6agNVWr56V0  
       Oct 20, 2016
    @ansheng 那就参考 6 楼的链接吧,不论啥语言的 Socket 功能,都是对操作系统的 socket 接口做的封装
    ansheng
        27
    ansheng  
    OP
       Oct 20, 2016
    @GeekGao 我觉得 14 楼的挺好的,
    R4rvZ6agNVWr56V0
        28
    R4rvZ6agNVWr56V0  
       Oct 20, 2016
    @ansheng 那只是网络服务模型,不是你提到的“ socket 工作原理”
    julyclyde
        29
    julyclyde  
       Oct 20, 2016
    @ansheng 技术不需要作比喻,技术就是技术。如果你需要比喻才能理解,那你其实还没理解
    ibigbug
        30
    ibigbug  
       Oct 21, 2016
    两个关键字:三次握手, CLOSE_WAIT 。可以了解各大概了。

    这玩意儿很难一下子理解透彻,遇到问题对着状态图多看,自己写个 tcp echo client/server 多试。
    Allianzcortex
        31
    Allianzcortex  
       Oct 21, 2016
    直接动手就大块头的书太过分了。。如果是要快速理解的话推荐看看 tutorialspoint 家的教程 https://www.tutorialspoint.com/unix_sockets/network_addresses.htm ,如果不做服务器端开发基本不会用到的, Web 框架里面都封装好了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   6147 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 105ms · UTC 06:21 · PVG 14:21 · LAX 23:21 · JFK 02:21
    ♥ Do have faith in what you're doing.