python socket send 错误:TypeError: 'str' does not support the buffer interface
server.py========================importsockets=socket.socket(socket.AF_INET,socket.SO...
server.py
========================
import socket
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('localhost',2000))
s.listen(5)
while 1:
cs,address = s.accept()
print('Client:',address)
cs.send('OK')
print(cs.recv(1024))
cs.close()
==============================
client.py
==============================
import socket
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('127.0.0.1',2000))
data=s.recv(1024)
print('Copy That',data)
s.send('Hello')
s.close()
===========================
运行结果:
server:
E:\wang\code\http>server.py
Client: ('127.0.0.1', 3316)
Traceback (most recent call last):
File "E:\wang\code\http\server.py", line 10, in <module>
client.py
===================
E:\wang\code\http>client.py
Copy That b''
Traceback (most recent call last):
File "E:\wang\code\http\client.py", line 6, in <module>
s.send('Hello')
TypeError: 'str' does not support the buffer interface
=============================
都是 .send 的时候出现.TypeError: 'str' does not support the buffer interface
请问,这个问题是因为什么呢.
我是按照教程码出来的代码.
因为我用的是python3.2么.
那要如何修改?
谢谢.已经正常运行.
但是现在, 服务端收到的值是 b'Hello' , 客户端收到的是: b'OK'
难道要手动把b'' 去掉,取其中的值么???
import socket
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('localhost',2000))
s.listen(5)
while 1:
cs,address = s.accept()
print('Client:',address)
cs.send(b'OK')
print(repr(cs.recv(1024)))
cs.close()
=====================================
好了.我自己搞定了.
输出的时候给弄个编码, 不然会输出类型.
print(cs.recv(1024).decode('utf-8')) 展开
========================
import socket
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('localhost',2000))
s.listen(5)
while 1:
cs,address = s.accept()
print('Client:',address)
cs.send('OK')
print(cs.recv(1024))
cs.close()
==============================
client.py
==============================
import socket
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('127.0.0.1',2000))
data=s.recv(1024)
print('Copy That',data)
s.send('Hello')
s.close()
===========================
运行结果:
server:
E:\wang\code\http>server.py
Client: ('127.0.0.1', 3316)
Traceback (most recent call last):
File "E:\wang\code\http\server.py", line 10, in <module>
client.py
===================
E:\wang\code\http>client.py
Copy That b''
Traceback (most recent call last):
File "E:\wang\code\http\client.py", line 6, in <module>
s.send('Hello')
TypeError: 'str' does not support the buffer interface
=============================
都是 .send 的时候出现.TypeError: 'str' does not support the buffer interface
请问,这个问题是因为什么呢.
我是按照教程码出来的代码.
因为我用的是python3.2么.
那要如何修改?
谢谢.已经正常运行.
但是现在, 服务端收到的值是 b'Hello' , 客户端收到的是: b'OK'
难道要手动把b'' 去掉,取其中的值么???
import socket
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('localhost',2000))
s.listen(5)
while 1:
cs,address = s.accept()
print('Client:',address)
cs.send(b'OK')
print(repr(cs.recv(1024)))
cs.close()
=====================================
好了.我自己搞定了.
输出的时候给弄个编码, 不然会输出类型.
print(cs.recv(1024).decode('utf-8')) 展开
1个回答
展开全部
python3.2 socket.send 修改传送数据必须是bytes
http://docs.python.org/py3k/library/socket.html
改成 s.send(b'Hello')
这里有官网的例子:
# 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 True:
data = conn.recv(1024)
if not data: break
conn.send(data)
conn.close()
# Echo client program
import socket
HOST = 'daring.cwi.nl' # 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.send(b'Hello, world')
data = s.recv(1024)
s.close()
print('Received', repr(data))
http://docs.python.org/py3k/library/socket.html
改成 s.send(b'Hello')
这里有官网的例子:
# 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 True:
data = conn.recv(1024)
if not data: break
conn.send(data)
conn.close()
# Echo client program
import socket
HOST = 'daring.cwi.nl' # 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.send(b'Hello, world')
data = s.recv(1024)
s.close()
print('Received', repr(data))
追问
谢谢.已经正常运行.
但是现在, 服务端收到的值是 b'Hello' , 客户端收到的是: b'OK'
难道要手动把b'' 去掉,取其中的值么???
import socket
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('localhost',2000))
s.listen(5)
while 1:
cs,address = s.accept()
print('Client:',address)
cs.send(b'OK')
print(repr(cs.recv(1024)))
cs.close()
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询