typeError:需要一个字节状对象,而不是str'在我的服务器中

发布于 2025-01-20 04:45:06 字数 1220 浏览 0 评论 0原文

当我创建服务器和客户端时,我无法向它们发送命令 此代码服务器用于连接 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请持续率性 2025-01-27 04:45:06

套接字编程始终以字节为单位传输数据(八个 0 和 1 的集合)
发送时,发送的数据必须是字节格式。

arr = bytes(mssg, 'utf-8')

或者

arr = bytes(mssg, 'ascii')

可以将您的消息转换为字节,然后发送。

在接收端,将这些字节转换回 str 对象

mssg = recieved.decode("utf-8")

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.

arr = bytes(mssg, 'utf-8')

or

arr = bytes(mssg, 'ascii')

can convert your message to bytes, then send this.

On receiving end, Convert these bytes back to str object with

mssg = recieved.decode("utf-8")
长安忆 2025-01-27 04:45:06

实际上,v.send() 需要一个类似字节的对象,而不是 str,因此 input() 的值是 str ,所以它不能是。你也可以使用这个:

v.send(b”msg” + b” “ * (128 - len(b”msg”)))

这样如果可以接收多条消息,因为所有消息 len 是用于接收的 64 个字母

v.recv(64)

您还可以创建一个如下函数:

def send(msg):
    sendMsg = msg.encode()
    sendMsg += b” “ * (64 - len(sendMsg))
    v.send(msg)

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:

def send(msg):
    sendMsg = msg.encode()
    sendMsg += b” “ * (64 - len(sendMsg))
    v.send(msg)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文