Archive for November, 2010

Python Server and Client Tool

This is the server side script.
[Python]
#~ ///////////////////////////////////////////////////////////////////////////////
#~ //
#~ // Copyright (c) 2010-2011, Kurian OS
#~ // All rights reserved.
#~ //
#~ // Redistribution and use in source and binary forms, with or without
#~ // modification, are permitted provided that the following conditions
#~ // are met:
#~ //
#~ // Redistributions of source code must retain the above copyright
#~ // notice, this list of conditions and the following disclaimer.
#~ // Redistributions in binary form must reproduce the above copyright
#~ // notice, this list of conditions and the following disclaimer in the
#~ // documentation and/or other materials provided with the
#~ // distribution. Neither the name of Kurian Os nor the
#~ // names of its contributors may be used to endorse or promote
#~ // products derived from this software without specific prior written
#~ // permission.
#~ //
#~ // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
#~ // “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
#~ // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
#~ // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
#~ // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
#~ // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
#~ // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
#~ // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
#~ // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
#~ // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
#~ // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
#~ // OF THE POSSIBILITY OF SUCH DAMAGE.
#~ //
#~ ///////////////////////////////////////////////////////////////////////////////
from socket import *
import sys
import os
from threading import Thread
import shutil
commandBuf = 4096
commandHost = ”
commandPort = 78569
commandAddress = (commandHost,commandPort)
class serverFunctions(Thread):
def __init__(self):
Thread.__init__(self)
self.setupServerCmd()

def setupServerCmd(self):
self.serverSocket = socket( AF_INET,SOCK_STREAM)
try:
self.serverSocket.bind((commandAddress))
self.currentClient = None
self.serverListernNum = 0
self.serverProcessing = 0
self.listenAndRun()
except:
self.closeConnections()

def listenAndRun(self):
self.serverProcessing = 1
while self.serverProcessing:
self.listenToClient()
self.serverProcessing = 1
while self.serverProcessing:
self.serverClientProcessCmd()
self.currentClient.close()
self.serverSocket.close()

def listenToClient(self):
self.serverSocket.listen(15)
print ‘…listening’
clientName,clientAddress = self.serverSocket.accept()
self.currentClient = clientName
print ‘…connected: ‘, clientAddress

def serverClientProcessCmd(self):
parseCommad = self.currentClient.recv(commandBuf)
if not parseCommad: return
self.serverCommandPrs(parseCommad)
if self.serverProcessing:
getCommands = parseCommad.split(‘ ‘)
if len(getCommands) < 2: processInfo = os.popen(parseCommad) outputInfo = processInfo.read() self.currentClient.send(str(outputInfo)) else: systemCommand = getCommands[0] sourceFile = getCommands[1] destiFile = getCommands[2] try: copyFilePath = shutil.copy(sourceFile,destiFile) self.currentClient.send('%s copied to %s'%(sourceFile,destiFile)) except Exception as errorDetails: outputInfo = errorDetails self.currentClient.send(str(outputInfo)) self.restartServerOnError() def serverCommandPrs(self,cmd): cmd = cmd.strip() if cmd == 'resetPeer': self.serverProcessing = 0 self.closeConnections() serv = ServCmd() elif cmd == 'downSrv': self.serverProcessing = 0 self.closeConnections() def restartServerOnError(self): self.closeConnections() serv = serverFunctions() def closeConnections(self): try: self.currentClient.close() self.serverSocket.close() except: pass if __name__ == '__main__': serv = serverFunctions() [/Python] This is client side script . [Python] #~ /////////////////////////////////////////////////////////////////////////////// #~ // #~ // Copyright (c) 2010-2011, Kurian OS #~ // All rights reserved. #~ // #~ // Redistribution and use in source and binary forms, with or without #~ // modification, are permitted provided that the following conditions #~ // are met: #~ // #~ // Redistributions of source code must retain the above copyright #~ // notice, this list of conditions and the following disclaimer. #~ // Redistributions in binary form must reproduce the above copyright #~ // notice, this list of conditions and the following disclaimer in the #~ // documentation and/or other materials provided with the #~ // distribution. Neither the name of Kurian Os nor the #~ // names of its contributors may be used to endorse or promote #~ // products derived from this software without specific prior written #~ // permission. #~ // #~ // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS #~ // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT #~ // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS #~ // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE #~ // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, #~ // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES #~ // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR #~ // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) #~ // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, #~ // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) #~ // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED #~ // OF THE POSSIBILITY OF SUCH DAMAGE. #~ // #~ /////////////////////////////////////////////////////////////////////////////// from socket import * from time import time from time import sleep import maya.cmds as cmds import sys clientBuf = 4096 class clientCommand: def __init__(self,serverHostAddress): self.serverHost = serverHostAddress self.serverPort = 78569 self.serverAddeess = (self.serverHost,self.serverPort) self.serverSocekt = None def createServerConnection(self): self.serverSocekt = socket( AF_INET,SOCK_STREAM) self.serverSocekt.connect(self.serverAddeess) def sendServerCommand(self,serverCommand): self.serverSocekt.send(serverCommand) def getResponse(self): dataFromServer = self.serverSocekt.recv(clientBuf) print dataFromServer if __name__ == '__main__': #~ user your server address or ip here like #~ clientConnection = clientCommand('mainserver') or #~ clientConnection = clientCommand('192.168.1.1') clientConnection = clientCommand('localhost') clientConnection.createServerConnection() #~ use commands like any system commands #~ clientConnection.sendServerCommand('ifconfig') #~ or #~ now we just creating some custom commands to copy some files #~ for windows #~ clientConnection.sendServerCommand('copy C:/temp/test.txt C:/temp/test_copy') #~ or #~ clientConnection.sendServerCommand('copy C:\\temp\\test.txt C:\\temp\\test_copy') getCurrentFilePath = cmds.file(loc=True,q=True) if getCurrentFilePath == 'unknown': print 'File not saved !' raise RuntimeError('File not saved !') clientConnection.sendServerCommand('copy %s /home/kurian/python_scripts/test_copy'%getCurrentFilePath) clientConnection.getResponse() #~ use the resetPeer command to reset the server to accept another file #clientConnection.sendServerCommand('resetPeer') #~ use only with admin right #~ conn.sendCmd('downSrv') [/Python]