WartY - again - | - komisches verhalten bei socket.recv

Moinsen :D

Ich habe endlich wieder an meinem WartY gearbeitet.

Funzt auch eig. ganz gut.
Jetzt treten probleme in folgender form auf:

Code:
crac@crac-desktop:~/Dokumente$ python do.py

======================================
 WartY Server-Client VS 0.2 - Welcome!
======================================

Enter Commands please! (hlp for help)
connect
IP-Adresse: 
Port: 
Got you on ('127.0.0.1', 37749)
> uptime
 23:51:40 up  5:13,  3 users,  load average: 0.98, 0.95, 0.97

> lsusb
 23:51:40 up  5:13,  3 users,  load average: 0.98, 0.95, 0.97

> lsusb

Das '>' ist ein Command-prompt dings :D
folglich meine eingaben

Zur selben zeit beim server app:

Code:
crac@crac-desktop:~/Dokumente$ python a.py
('running command:', 'uptime')

wie man sieht wird nur das erste command geparst...

hier der quellcode:

Client -do.py
Code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Made by: CrAc / ZeeX
#Whats WartY?:
#WartY is made, to connect to a Server for remote administration.
#You have to verify and type a password...
#after that you're able to command the Server how you supposed to do
#-----------------------------
#        finally: s.close()
#while True: 
#print ("Connected to Host")
# s.recv(1024)
#print "[%s] %s" % (ip,antwort)
#-----------------------------
print "\n======================================\n WartY Server-Client VS 0.2 - Welcome!\n======================================\n"
import socket
import sys
import os
import subprocess

#connect to server
def connect():
    
    port = 1111
    ip = raw_input("IP-Adresse: ") or "127.0.0.1" 
    port_int = raw_input("Port: ")
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
    try:
        s.connect((ip, port))

    except socket.error, (value,message): 
        print ("Unable to connect:\n" + message +"\nPlease Try again:\n")
        connect()
    try: 
        while True:
            data = s.recv(1024)
	    print data
            data = raw_input('> ')
            if not data:
		break
            s.send(data)
    finally:
        s.close()

#command prompt on startup
def prompt():

    while True:
        inpt = raw_input("Enter Commands please! (hlp for help)\n")
        
        if inpt == "hlp":
            print ("commands:\n'hlp'.....help\n'makesrv'.....create new Server\n'exit'.....exit Warty\n'info'.....get information about WartY and its usage")
        
        elif inpt == "makesrv":
            makesrv()

        elif inpt == "connect":
            connect()
            
        elif inpt == "info":
            info()

        elif inpt == "exit":
            sys.exit()
            
        else: 
            print "Wrong command - please verify if command is valid!"

if __name__ == "__main__":
    prompt()

Server a.py

Code:
#!/usr/bin/python
# -*- coding: utf-8 -*-

import socket
import threading, thread
import os
import subprocess
import sys
import logging
PORT = 1111
PASS = 'passw'

logging.basicConfig (
    filename = "WartY.log", 
    filemode = "a",
    level = logging.DEBUG,
    format = "%(asctime)s [%(levelname)-8s] %(module)s, %(funcName)s, %(lineno)s -|- %(message)s" ) 
logging.info("Session started")

closeMessage = ("Quit Warty...")

#run received commands
def exe(s,command):
    process = subprocess.Popen(command, stdout=subprocess.PIPE)
    process.wait()
    data =  process.stdout.read()
    logging.info(data)
    for line in data.split('\n'):
        s.send(data)

#accept connections
def con():
    listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)#make socket

    try:
	logging.info("listener bind on", PORT)#writing to logfile
        listener.bind(('',PORT))#bin socket on port
    except ValueError,e:
        logging.error(e)#writing error to logfile
        print e#print error

    listener.listen(5)#socket is listening

    while True:
        s,addr = listener.accept()#accept incoming connections
        s.send('Got you on %s' % str(addr))
	logging.info("Connection from: %s" % str(addr))
        command = s.recv(1024)
        if command >= 0:
            try: 
		print ("running command:", command)
		logging.info("command:", command)
		exe(s,command)

            except OSError, e:
		logging.error("Execution failed:", e)
                print >>sys.stderr, "Execution failed:", e
    logging.info("Socket closed")
    s.close()

if __name__ == "__main__": 
    con()

Danke im vorraus, und sry für den riesigen Thread :D

/e: Quellcode vom Client beschnitten
 
Ich kann zwar kein Phyton aber ich glaub der Fehler liegt hier:

Code:
while True:
        s,addr = listener.accept()#accept incoming connections
        s.send('Got you on %s' % str(addr))
	logging.info("Connection from: %s" % str(addr))
        command = s.recv(1024)
        if command >= 0:
            try: 
		print ("running command:", command)
		logging.info("command:", command)
		exe(s,command)

            except OSError, e:
		logging.error("Execution failed:", e)
                print >>sys.stderr, "Execution failed:", e

Accept wird nur einmal aufgerufen um eben den Client zu "akzeptieren". Danach ist die Verbindung hergestellt und muss nicht nochmal akzeptiert werden. So wie es aussieht lässt du pro Verbindung nur ein Command ausführen. Also so wird es eher funktionieren:

Code:
      s,addr = listener.accept()#accept incoming connections
      s.send('Got you on %s' % str(addr))
      logging.info("Connection from: %s" % str(addr))
      while True:
        command = s.recv(1024)
        if command >= 0:
            try: 
		print ("running command:", command)
		logging.info("command:", command)
		exe(s,command)

            except OSError, e:
		logging.error("Execution failed:", e)
                print >>sys.stderr, "Execution failed:", e

Und: While True - ist kein guter Programmierstil ;)
 
Wow danke :D

hundertprozentig ises immer noch ncih :D

Code:
crac@crac-desktop:~/Dokumente$ python do.py

======================================
 WartY Server-Client VS 0.2 - Welcome!
======================================

Enter Commands please! (hlp for help)
connect
IP-Adresse: 
Port: 
Got you on ('127.0.0.1', 55039)
> uptime
 17:22:06 up 17 min,  3 users,  load average: 1.63, 1.31, 0.85

> pwd
 17:22:06 up 17 min,  3 users,  load average: 1.63, 1.31, 0.85

> lsusb
/home/crac/Dokumente
/home/crac/Dokumente

> uptime
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 001 Device 0
> lspci
01: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
 17:22:51 up 17 min,  3 users,  load average: 1.15, 1.22, 0.84
 17:22:51 up 17 min,  3 users,  load average: 1.15, 1.22, 0.84

>

xD

is while 1 besser?

is mein erstes programm in python, deswegen habe ich da keine erfahrung mit :D
wüsst auch keine alternative...

lg

/e liegt definitiv am client, da die log des servers sauber ist:

Code:
2009-06-17 17:33:08,217 [INFO    ] a, <module>, 18 -|- Session started
2009-06-17 17:33:12,856 [INFO    ] a, con, 45 -|- Connection from: ('127.0.0.1', 35753)
2009-06-17 17:33:15,718 [INFO    ] a, con, 52 -|- uptime
2009-06-17 17:33:15,726 [INFO    ] a, exe, 27 -|-  17:33:15 up 28 min,  3 users,  load average: 0.27, 0.45, 0.60

2009-06-17 17:33:17,754 [INFO    ] a, con, 52 -|- pwd
2009-06-17 17:33:17,763 [INFO    ] a, exe, 27 -|- /home/crac/Dokumente

2009-06-17 17:33:21,954 [INFO    ] a, con, 52 -|- lsusb
2009-06-17 17:33:22,285 [INFO    ] a, exe, 27 -|- Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub

die schleife fürs abfangen und senden sieht momentan so aus:

Code:
        while True:
            data = s.recv(1024)
	    print data
            data = raw_input('> ')
            """if not data:
		break"""
            s.send(data)
 
OT:
Als Übung mag das ganze ja gut sein, aber für den wirklichen Einsatz würde ich dir wirklich zu ssh raten. Damit ersparst du dir einen Haufen Aufwand.
 
Zurück
Oben