[Twisted-Python] Re: DOUBTS PLEASE SOLVE

shweta mani shweta3230 at gmail.com
Thu Oct 2 02:10:52 EDT 2008


hi all i got the solution of listing all ls -l files in a listbox .
Next problem is to use recurrsively ls -l command again if a user selects a
directory.
Can we use multiple in Linux commands within a program withoy using pipe.
i am forwarding the code.
i need to use ls -l command dependng on whether he chooses a directory.

#!/usr/bin/env python
from twisted.conch.ssh import transport, userauth, connection, common, keys,
channel
from twisted.internet import defer, protocol, reactor
from twisted.python import log
import struct, sys, getpass, os
import re
import Tkinter
from Tkinter import *
from tkMessageBox import *
from os.path import exists
from Tkinter import *
import commands
#from DT import demo
import shutil
from os.path import exists
import ftplib
import sys
from ftplib import FTP
from tkMessageBox import *
from tkFileDialog import asksaveasfilename
import string
from os import popen
from re import split
import Tkinter
import os
USER = 'cteam'  # replace this with a valid username
HOST = '10.144.17.2' # and a valid host

class SimpleTransport(transport.SSHClientTransport):
    def verifyHostKey(self, hostKey, fingerprint):
        print 'host key fingerprint: %s' % fingerprint
        return defer.succeed(1)
    def connectionSecure(self):
        self.requestService(
            SimpleUserAuth(USER,
                SimpleConnection()))
class SimpleUserAuth(userauth.SSHUserAuthClient):
    def getPassword(self):
        return defer.succeed(getpass.getpass("%s@%s's password: " % (USER,
HOST)))
    def getGenericAnswers(self, name, instruction, questions):
        print name
        print instruction
        answers = []
        for prompt, echo in questions:
            if echo:
                answer = raw_input(prompt)
            else:
                answer = getpass.getpass(prompt)
            answers.append(answer)
        return defer.succeed(answers)

    def getPublicKey(self):
        path = os.path.expanduser('~/.ssh/id_dsa')
        # this works with rsa too
        # just change the name here and in getPrivateKey
        if not os.path.exists(path) or self.lastPublicKey:
            # the file doesn't exist, or we've tried a public key
            return
        return keys.getPublicKeyString(path+'.pub')
    def getPrivateKey(self):
        path = os.path.expanduser('~/.ssh/id_dsa')
        return defer.succeed(keys.getPrivateKeyObject(path))
class SimpleConnection(connection.SSHConnection):
    def serviceStarted(self):
        self.openChannel(TrueChannel(2**16, 2**15, self))
        self.openChannel(FalseChannel(2**16, 2**15, self))
        self.openChannel(CatChannel(2**16, 2**15, self))
class TrueChannel(channel.SSHChannel):
    name = 'session' # needed for commands
    def openFailed(self, reason):
        print 'true failed', reason

    def channelOpen(self, ignoredData):
        self.conn.sendRequest(self, 'exec', common.NS('true'))
    def request_exit_status(self, data):
        status = struct.unpack('>L', data)[0]
        print 'true status was: %s' % status
        self.loseConnection()
class FalseChannel(channel.SSHChannel):
    name = 'session'
    def openFailed(self, reason):
        print 'false failed', reason
    def channelOpen(self, ignoredData):
        self.conn.sendRequest(self, 'exec', common.NS('false'))
    def request_exit_status(self, data):
        status = struct.unpack('>L', data)[0]
        print 'false status was: %s' % status
        self.loseConnection()
class CatChannel(channel.SSHChannel):
    name = 'session'
    def openFailed(self, reason):
        print 'echo failed', reason
    def channelOpen(self, ignoredData):
        self.data = ''
        d = self.conn.sendRequest(self, 'exec', common.NS('ls -l'),
wantReply = 1)
        d.addCallback(self._cbRequest)

    def _cbRequest(self, ignored):
        self.write('hello conch\n')
        self.conn.sendEOF(self)
    def dataReceived(self, data):
        self.data=self.data+data

    def closed(self):
        win = Tkinter.Tk();
        Label(win,text='CARD UPLOAD UTILITY').pack(side=TOP)

        dirfm = Frame(win)
        dirsa=Scrollbar(dirfm)
        dirsb= Scrollbar(dirfm)
        dirsb.pack(side = RIGHT, fill =Y)

        dirs = Listbox(dirfm,height = 50, width = 70, yscrollcommand =
dirsb.set)
        textsfile = Entry(dirfm)
        print 'got data from listing: %s' % self.data
        recs=self.data.split('\n')
        '''p4 = re.sub('[]''""['']','','%s'%recs)
        print 'p4[0]',p4[0]'''

        print 'shweta',recs
        var=StringVar();
        for line in recs[1:]:

            p4 = re.sub('[]''""['']','','%s'%line)
            print p4[0]

            p2 = string.split(p4)
            print 'p2',p2
            p5 = []
            p5 = p5.append(p2[8])
            print line
            w=p4[0] # d r _
            dirs.insert(END,line)
        var=StringVar();
        #dirs.bind('<Double-1>', listd)

        textsfile.pack( side = BOTTOM)
        dirsb.config(command=dirs.yview)
        dirs.pack(side=LEFT,fill=BOTH)
        c=textsfile.get()
        dirfm.pack()
        self.loseConnection()
        reactor.stop()


protocol.ClientCreator(reactor, SimpleTransport).connectTCP(HOST, 22)
reactor.run()



the next problem is to execute a script from the server which accepts inputs
from the user and processes it.
for eg sh test1.sh
On Tue, Sep 30, 2008 at 9:34 PM, Jean-Paul Calderone <exarkun at divmod.com>wrote:

> On Tue, 30 Sep 2008 21:24:03 +0530, shweta mani <shweta3230 at gmail.com>
> wrote:
>
>> [snip]
>>
>
> This isn't the right way to use Tk with Twisted.
>
> See
> http://twistedmatrix.com/projects/core/documentation/howto/choosing-reactor.html#auto14
>
> Jean-Paul
>
>
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20081002/b6b08aff/attachment.htm 


More information about the Twisted-Python mailing list