Python远程控制不起作用

时间:2016-02-09 15:16:04

标签: python windows sockets client-server remote-client

您好我写了一个工具来远程控制我的电脑。但它只是不起作用。 我得到服务器和客户端之间的连接,但如果我发送命令,他们不会被执行。谢谢你的帮助。我测试了客户端和服务器分离,我认为问题出在服务器上(它有一个客户端套接字 - 对此感到困惑) - 它不执行批处理命令

服务器:

import socket, os, time
tester = 1

def main():
    conn = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    while True:
        time.sleep(1)
        try:
            conn.connect(("internalip",50001))
            income()
        except:
            main()



def income():
    while True:
        befehl = conn.recv(2048)
        print(befehl)
        if(befehl=="upload"):
            upload()

        elif(befehl=="download"):
            download()

        elif(befehl=="cmd"):
            cmdexe()

        else:
            conn.send("unknown command")


def cmdexe():    
    command = conn.recv(2048)
    print(command)
    if(command=="exit"):
        income()

    else:
        try:
            os.system(command)
        except:
            conn.send(str(exception)+" problem with cmd")
    income()


def upload():
    name=str(conn.recv(2048))
    if(name=="exit"):
        income()
    else:
        mode==str(conn.recv(1024))
        if(mode=="exit"):
            income()
        else:
            f = open(name,mode)
            try:
                f.write(str(conn.recv(8192)))
                f.close()
            except:
                conn.send("An Error with filehandling/upload")
                f.close()
                income()



def download():
    name=str(conn.recv(2048))
    if(name=="exit"):
        income()

    f= open(name,"r")
    try:
        conn.send(str(f.read()))
        income()
    except:
        f.close()
        conn.send("An Error with filehandling/upload")
        income()

#verbesserung: 1.automatisches setup 2.mehrere commands bei cmd

main()` 

客户端:

import socket
from threading import Thread

s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind(("",50001))
s.listen(1)

conn,addr = s.accept()

def main():
    print(str(addr)+" is connected")
    print("Choose a command")
    #command_list={"upload", "download", "cmd"}
    #for i in range(3):
    #    print(str(command_list[i]))
    print("upload", "download", "cmd")

    befehl=raw_input(": ")

    if(befehl=="exit"):
        main()
    elif(befehl=="upload"):
        upload()
    elif(befehl=="download"):
        download()
    elif(befehl=="cmd"):
        cmd()
    else:
        print("Sorry, thats not a regular command")



def upload():
    conn.send("upload")

    name=raw_input("Geben sie den Namen ein, den die Datei haben soll!")
    if(name=="exit"):
        conn.send(name)
        main()
    conn.send(name)

    mode=raw_input("Send a for append or w to write: ")
    if(name=="exit"):
        main()
    elif(mode=="a"):
        conn.send("a")
    elif(mode=="w"):
        conn.send("w")
    else:
        print("Thats not a regulare mode")
        conn.send("exit")
    uf = open(raw_input("Choose the file you want to send: "),"r")
    conn.send("uf")
    main()

"""
def errorhandling():
    print(str(conn.recv(4096)))

Thread(target=errorhandling()).start()
"""
def download():
    conn.send("download")
    name=raw_input("Geben sie den Namen, der Datei ein, die sie downloaden moechten: ")
    if(name==exit):
        conn.send(name)
        main()
    conn.send(name)
    try:
        f=open(addr+"; "+name,"a")
        f.append(conn.recv(1048576))
    except:
        print("Error with filehandling: "+exception)
        main()

def cmd():
    conn.send("cmd")
    command = raw_input("Auszufuehrendes Kommando: ")
    if(command=="exit"):
        conn.send("exit")
        main()
    conn.send(command)
    cmd()


#verbesserung: Probleme mit mehreren Verbindungen 



main()

1 个答案:

答案 0 :(得分:0)

您没有使用Fabric等现有工具的原因。

请参阅:http://www.fabfile.org/

Fabric是一个Python(2.5-2.7)库和命令行工具,用于简化SSH在应用程序部署或系统管理任务中的使用。

它提供了一套基本的操作套件,用于执行本地或远程shell命令(通常或通过sudo)和上传/下载文件,以及辅助功能,如提示正在运行的用户输入或中止执行。

相关问题