推荐学习书目
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
tanl
V2EX  ›  Python

请教各位 Python 中的 Socket 编程

  •  
  •   tanl · Oct 16, 2017 · 3533 views
    This topic created in 3147 days ago, the information mentioned may be changed or developed.

    最近在学习计算机网络自顶向下这本书 看到书本 P107 开始讲的套接字编程 我照着书本打了一遍代码,测试下来在本机(也就是 ip 127.0.0.1 )可以运行 但是我把 UDPServer.py 放到我的阿里云服务器(并将 hostname 改为服务器公网 ip )上运行,UDPClient 就没反应了?请问是什么原因呢?

    客户端

    from socket import *
    serverName = "106.14.209.139"
    serverPort = 10002
    clientSocket = socket(AF_INET,SOCK_DGRAM)
    message = raw_input("Input lowercase sentence:")
    clientSocket.sendto(message,(serverName, serverPort))
    modifiedMessage, serverAddress = clientSocket.recvfrom(2048)
    print serverAddress
    print modifiedMessage
    clientSocket.close()
    

    阿里云 ubuntu 服务器上

    from socket import *
    serverPort = 88
    serverSocket = socket(AF_INET,SOCK_DGRAM)
    serverSocket.bind(("", serverPort))
    print "The server is ready to receive"
    while True:
            message, clientAddress = serverSocket.recvfrom(2048)
            print clientAddress,message
            modifiedMessage = message.upper()
            serverSocket.sendto(modifiedMessage, clientAddress)
    
    13 replies    2017-10-17 14:18:34 +08:00
    wcsjtu
        1
    wcsjtu  
       Oct 16, 2017
    防火墙端口开了没
    tanl
        2
    tanl  
    OP
       Oct 16, 2017
    上面第二段代码端口号忘改了,改成 10002
    tanl
        3
    tanl  
    OP
       Oct 16, 2017
    @wcsjtu 是要开启 windows 上的 10002 端口吗?
    nosilence
        4
    nosilence  
       Oct 16, 2017
    大概率情况,你的阿里云的防火墙策略组没配置,端口没开。
    wcsjtu
        5
    wcsjtu  
       Oct 16, 2017
    @tanl -.-! 我记得阿里云是要自己开放服务端口的。
    Hzzone
        6
    Hzzone  
       Oct 16, 2017 via iPhone
    服务器 bind 的端口和客户端发送的端口都不是同一个。。。
    wcsjtu
        7
    wcsjtu  
       Oct 16, 2017
    @tanl localhot 能够访问的服务,其他主机不能访问的话,一般就 3 个原因:1. 网卡绑错了 2. 防火墙端口没开 3. 主机不通
    tanl
        8
    tanl  
    OP
       Oct 16, 2017
    @Hzzone 这个复制错了,我在上面说明了
    tanl
        9
    tanl  
    OP
       Oct 16, 2017
    @wcsjtu 我去好坑..成功了,要自己去阿里云官网配安全组,谢谢啦
    tanl
        10
    tanl  
    OP
       Oct 16, 2017
    @nosilence 感谢
    rogwan
        11
    rogwan  
       Oct 16, 2017 via Android
    阿里云的经典网络直接可以访问没问题,专有网络需要自己配置端口,才有访问权限。
    ksupertu
        12
    ksupertu  
       Oct 16, 2017 via iPhone
    安全组没放端口策略
    crazykuma
        13
    crazykuma  
       Oct 17, 2017
    借楼问一下,python 的 socket 编程跟其他语言比速度可以接受吗?会不会慢很多?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2452 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 15:52 · PVG 23:52 · LAX 08:52 · JFK 11:52
    ♥ Do have faith in what you're doing.