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

python 的 socket.recv 返回值的疑问

  •  
  •   xcc7624 · 2016-05-26 09:03:12 +08:00 · 6158 次点击
    这是一个创建于 2892 天前的主题,其中的信息可能已经有所发展或是发生改变。

    socket 的API文档

    python socket

    socket.recv(bufsize[, flags])


    Receive data from the socket. The return value is a string representing the data received. 
    The maximum amount of data to be received at once is specified by bufsize. See the Unix manual page recv(2) for the meaning of the optional argument flags; it defaults to zero.
    

    这里说 return value is a string representing 是什么意思?
    

    如下的程序,客户端为什么 print data 打印出的是乱码,这个不是 ASCII 码表示的 510 吗? -------------------------------------客户端------------------------- `

    Echo client program

    import socket

    HOST = '127.0.0.1' # The remote host PORT = 50007 # The same port as used by the server s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((HOST, PORT)) s.sendall('\x05\x01\x00') data = s.recv(1024) s.close()

    print data print 'Received', repr(data) ---------------------------------服务器端--------------------------------------

    Echo server program

    import socket

    HOST = '' # Symbolic name meaning all available interfaces PORT = 50007 # Arbitrary non-privileged port s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((HOST, PORT)) s.listen(1) conn, addr = s.accept() print 'Connected by', addr while 1: data = conn.recv(1024) if not data: break conn.sendall(data) conn.close() `

    2 条回复    2016-05-26 15:01:03 +08:00
    ms2008
        1
    ms2008  
       2016-05-26 09:43:09 +08:00
    当然乱码啊,你发的就是二进制流
    Muninn
        2
    Muninn  
       2016-05-26 15:01:03 +08:00
    你在本地能把 print('\x05\x01\x00') 打出来再说,汗。。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2879 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 11:37 · PVG 19:37 · LAX 04:37 · JFK 07:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.