如何将数据记录到字符串中 [英] How do I recv data to a string

查看:117
本文介绍了如何将数据记录到字符串中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的服务器创建自己的root shell,以便我可以在它的内侧工作,但是我总是试着检查输入==退出它只是不检查它和继续。对不起,这是拼错了,我不是英国人。我只是看不出我做错了什么。加上我试图与多个客户端合作,并将添加一个登录,我需要知道如何比较recv数据并检查它,以便登录工作,我只是不知道什么是出错。



我尝试了什么:



i am trying to make my own root shell for my server so that i can work on the in side of it, but ever time i try and check if the input == "exit" it just don't check it and continues. sorry is this is spelled wrong, i am not english. i just can't see what i am doing wrong. plus i am trying to have to work with more then one client and will be adding a login and well i need to know how to compare the recv data and check it so that the login works and well i just don't know whats going wrong.

What I have tried:

import socket
import threading
import json
import hashlib
from subprocess import PIPE, Popen

decode_utf8 = lambda data: data.decode("utf-8")

def main():
    s = socket.socket()
    s.bind(('127.0.0.1', 12344))
    s.listen(5)
    while True:
        r, addr = s.accept()
        print("Connected to by {}".format(str(addr)))
        threading.Thread(target=login(r)).start()

def login(r):
    read = open('data.json', 'r')
    data = json.load(read)
    while True:
        input_user = r.recv(1024)
        user = decode_utf8(input_user)
        try:
            password = data[str(user)]['password']
            input_user = r.recv(1024)
            pwd = decode_utf8(input_user)
            passdata = hashlib.sha256(pwd.encode().hexdigest()
            if passdata == password:
                rootShell(r)
            else:
                r.send('Wrong Password or Username')
                r.close()
        except:
            r.send('Wrong Password or Username')
            r.close()

def rootShell(r):
    while True:
        input_data = r.recv(1024)
        data = decode_utf8(input_data)
        print(data)
        try:
            if data.lower().startswith("exit"):
                r.send('Goodbye and see you later')
                r.close()
            else:
                r.send(cmdline(data))
        except:
            r.send("Error Running Command")

def cmdline(command):
    process = Popen(
        args=command,
        stdout=PIPE,
        shell=True
    )
    return process.communicate()[0]

main()

推荐答案

试试这个(没有保证,只是在黑暗中刺伤):



Try this (no guarantees, just a stab in the dark):

if (data.lower() == "exit"):





或者





or maybe

if (data.lower().startswith("exit")):

我找到了一种方法,通过将字节转换为十六进制并从字节中删除a0,然后将其转换回字符串



data = r.recv(1024)

data = codecs.encode(data,'hex')

data = str(data).replace('a0', '')

返回codecs.decode(数据,'hex')
I found a way of doing it by converting the bytes to hex and removing the "a0" at from the bytes and then convert it back to a string

data = r.recv(1024)
data = codecs.encode(data, ‘hex’)
data = str(data).replace(‘a0’, ‘’)
return codecs.decode(data, ‘hex’)


这篇关于如何将数据记录到字符串中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆