search

Home  >  Q&A  >  body text

python3.x - Python3 socket sendall appears 10057

When I was learning python to do a small socket example, I found that the following problems would occur as long as sendall was used.

server_demo:

#!/usr/bin/env python
# _*_coding:utf8_*_

import socket

obj_socket = socket.socket()
# 绑定端口
obj_socket.bind(('127.0.0.1', 9999))
# 设置最大连接数
obj_socket.listen(5)

while True:
    # 阻塞等待客户端连接
    conn, address = obj_socket.accept()
    print(address, conn)
    # 在python2.7中可以直接发送字符串,但是在python3中都是字节
    obj_socket.sendall(bytes('你好', encoding='utf-8'))

client_demo:


#!/usr/bin/env python
# _*_coding:utf8_*_
import socket

obj_socket = socket.socket()

obj_socket.connect(('127.0.0.1', 9999))
# 阻塞至服务器回复  最多接收1024bytes 如果超过会再次接收
res_bytes = obj_socket.recv(1024)
res_str = str(res_bytes, encoding='utf-8')

print(res_str)

obj_socket.close()

I searched for many solutions, and I tried turning off the firewall, but it still didn't work.

怪我咯怪我咯2754 days ago957

reply all(1)I'll reply

  • 某草草

    某草草2017-06-28 09:26:18

    Try it

    obj_socket.sendall(bytes('你好', encoding='utf-8'))
    换成:
    conn.sendall(bytes('你好', encoding='utf-8'))
    

    reply
    0
  • Cancelreply