typeError:需要一个字节状对象,而不是str'在我的服务器中
当我创建服务器和客户端时,我无法向它们发送命令 此代码服务器用于连接 clint
:-
import socket
s = socket.socket()
h="192.168.1.2"
p=665
s.bind((h, p))
print ("YOUR SESSION HAS BEEN CREATED IN PORT : ", p)
s.listen(1)
v, addr = s.accept()
print("SUCCESS CONECTION ...", addr)
mssge = input ("==> ")
while mssge != 'e':
v.send(mssge)
data = v.recv(1024)
print (data)
mssge = input ("==> ")
s.close()
我转到 deffrant 终端和 rin clint 代码 这段代码 clint :-
import subprocess
import socket
s= socket.socket()
h="192.168.1.2"
p=665
s.connect((h, p))
while True:
data=s.recv(1024)
if not data:
break
co = subprocess.Popen(data, shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
ct = co.stdout.read() + co.stderr.read()
cf = str(ct)
s.send(cf)
s.close()
连接后,当我正确地提出任何建议时,我遇到了问题 和这个错误:-
YOUR SESSION HAS BEEN CREATED IN PORT : 665
SUCCESS CONECTION ... ('192.168.1.2', 49508)
==> ls
Traceback (most recent call last):
File "/root/i.py", line 13, in <module>
v.send(mssge)
TypeError: a bytes-like object is required, not 'str'
请帮忙
When I create a server and a clint, I cannot send commands to them
this code server to connect clint
:-
import socket
s = socket.socket()
h="192.168.1.2"
p=665
s.bind((h, p))
print ("YOUR SESSION HAS BEEN CREATED IN PORT : ", p)
s.listen(1)
v, addr = s.accept()
print("SUCCESS CONECTION ...", addr)
mssge = input ("==> ")
while mssge != 'e':
v.send(mssge)
data = v.recv(1024)
print (data)
mssge = input ("==> ")
s.close()
and i go to deffrant terminal and rin clint code
and this code clint :-
import subprocess
import socket
s= socket.socket()
h="192.168.1.2"
p=665
s.connect((h, p))
while True:
data=s.recv(1024)
if not data:
break
co = subprocess.Popen(data, shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
ct = co.stdout.read() + co.stderr.read()
cf = str(ct)
s.send(cf)
s.close()
after connect when i right any commend I have a problem
and this error :-
YOUR SESSION HAS BEEN CREATED IN PORT : 665
SUCCESS CONECTION ... ('192.168.1.2', 49508)
==> ls
Traceback (most recent call last):
File "/root/i.py", line 13, in <module>
v.send(mssge)
TypeError: a bytes-like object is required, not 'str'
please some help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
套接字编程始终以字节为单位传输数据(八个 0 和 1 的集合)
发送时,发送的数据必须是字节格式。
或者
可以将您的消息转换为字节,然后发送。
在接收端,将这些字节转换回 str 对象
Socket Programming always transfers data in bytes(set of eight 0s and 1s)
When you send, the data you send must be of bytes format.
or
can convert your message to bytes, then send this.
On receiving end, Convert these bytes back to str object with
实际上,v.send() 需要一个类似字节的对象,而不是 str,因此 input() 的值是 str ,所以它不能是。你也可以使用这个:
v.send(b”msg” + b” “ * (128 - len(b”msg”)))
这样如果可以接收多条消息,因为所有消息 len 是用于接收的 64 个字母
:
v.recv(64)
您还可以创建一个如下函数:
Actually,
v.send()
requires a bytes-like object, not str, so that input()’s value is a str so that it cannot be. and you can also use this :v.send(b”msg” + b” “ * (128 - len(b”msg”)))
so that if multi-messages can be received because all message len are 64 letters
for receive:
v.recv(64)
you can also create a function like: