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
tanl
V2EX  ›  Python

请教各位 Python 中的 Socket 编程

  •  
  •   tanl · 2017-10-16 19:19:55 +08:00 · 2623 次点击
    这是一个创建于 2395 天前的主题,其中的信息可能已经有所发展或是发生改变。

    最近在学习计算机网络自顶向下这本书 看到书本 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 条回复    2017-10-17 14:18:34 +08:00
    wcsjtu
        1
    wcsjtu  
       2017-10-16 19:20:41 +08:00
    防火墙端口开了没
    tanl
        2
    tanl  
    OP
       2017-10-16 19:21:49 +08:00
    上面第二段代码端口号忘改了,改成 10002
    tanl
        3
    tanl  
    OP
       2017-10-16 19:22:23 +08:00
    @wcsjtu 是要开启 windows 上的 10002 端口吗?
    nosilence
        4
    nosilence  
       2017-10-16 19:23:00 +08:00
    大概率情况,你的阿里云的防火墙策略组没配置,端口没开。
    wcsjtu
        5
    wcsjtu  
       2017-10-16 19:23:46 +08:00
    @tanl -.-! 我记得阿里云是要自己开放服务端口的。
    Hzzone
        6
    Hzzone  
       2017-10-16 19:24:05 +08:00 via iPhone
    服务器 bind 的端口和客户端发送的端口都不是同一个。。。
    wcsjtu
        7
    wcsjtu  
       2017-10-16 19:25:50 +08:00
    @tanl localhot 能够访问的服务,其他主机不能访问的话,一般就 3 个原因:1. 网卡绑错了 2. 防火墙端口没开 3. 主机不通
    tanl
        8
    tanl  
    OP
       2017-10-16 19:26:12 +08:00
    @Hzzone 这个复制错了,我在上面说明了
    tanl
        9
    tanl  
    OP
       2017-10-16 19:36:32 +08:00
    @wcsjtu 我去好坑..成功了,要自己去阿里云官网配安全组,谢谢啦
    tanl
        10
    tanl  
    OP
       2017-10-16 19:36:52 +08:00
    @nosilence 感谢
    rogwan
        11
    rogwan  
       2017-10-16 20:18:55 +08:00 via Android
    阿里云的经典网络直接可以访问没问题,专有网络需要自己配置端口,才有访问权限。
    ksupertu
        12
    ksupertu  
       2017-10-16 23:08:35 +08:00 via iPhone
    安全组没放端口策略
    crazykuma
        13
    crazykuma  
       2017-10-17 14:18:34 +08:00
    借楼问一下,python 的 socket 编程跟其他语言比速度可以接受吗?会不会慢很多?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3777 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 04:44 · PVG 12:44 · LAX 21:44 · JFK 00:44
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.