{"id":349,"date":"2010-11-21T05:22:25","date_gmt":"2010-11-21T05:22:25","guid":{"rendered":"http:\/\/www.kurianos.com\/wordpress\/?p=349"},"modified":"2011-05-28T12:53:37","modified_gmt":"2011-05-28T12:53:37","slug":"python-server-and-client-tool","status":"publish","type":"post","link":"https:\/\/www.kurianos.com\/wordpress\/?p=349","title":{"rendered":"Python Server and Client Tool"},"content":{"rendered":"<p>This is the server side script.<br \/>\n[Python]<br \/>\n#~ \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<br \/>\n#~ \/\/<br \/>\n#~ \/\/ Copyright (c) 2010-2011, Kurian OS<br \/>\n#~ \/\/ All rights reserved.<br \/>\n#~ \/\/<br \/>\n#~ \/\/ Redistribution and use in source and binary forms, with or without<br \/>\n#~ \/\/ modification, are permitted provided that the following conditions<br \/>\n#~ \/\/ are met:<br \/>\n#~ \/\/<br \/>\n#~ \/\/ Redistributions of source code must retain the above copyright<br \/>\n#~ \/\/ notice, this list of conditions and the following disclaimer.<br \/>\n#~ \/\/ Redistributions in binary form must reproduce the above copyright<br \/>\n#~ \/\/ notice, this list of conditions and the following disclaimer in the<br \/>\n#~ \/\/ documentation and\/or other materials provided with the<br \/>\n#~ \/\/ distribution.  Neither the name of Kurian Os nor the<br \/>\n#~ \/\/ names of its contributors may be used to endorse or promote<br \/>\n#~ \/\/ products derived from this software without specific prior written<br \/>\n#~ \/\/ permission.<br \/>\n#~ \/\/<br \/>\n#~ \/\/ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS<br \/>\n#~ \/\/ &#8220;AS IS&#8221; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT<br \/>\n#~ \/\/ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS<br \/>\n#~ \/\/ FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE<br \/>\n#~ \/\/ COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,<br \/>\n#~ \/\/ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES<br \/>\n#~ \/\/ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR<br \/>\n#~ \/\/ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)<br \/>\n#~ \/\/ HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,<br \/>\n#~ \/\/ STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)<br \/>\n#~ \/\/ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED<br \/>\n#~ \/\/ OF THE POSSIBILITY OF SUCH DAMAGE.<br \/>\n#~ \/\/<br \/>\n#~ \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<br \/>\nfrom socket import *<br \/>\nimport sys<br \/>\nimport os<br \/>\nfrom threading import Thread<br \/>\nimport shutil<br \/>\ncommandBuf = 4096<br \/>\ncommandHost = &#8221;<br \/>\ncommandPort = 78569<br \/>\ncommandAddress = (commandHost,commandPort)<br \/>\nclass serverFunctions(Thread):<br \/>\n\tdef __init__(self):<br \/>\n\t\tThread.__init__(self)<br \/>\n\t\tself.setupServerCmd()<\/p>\n<p>\tdef setupServerCmd(self):<br \/>\n\t\tself.serverSocket = socket( AF_INET,SOCK_STREAM)<br \/>\n\t\ttry:<br \/>\n\t\t\tself.serverSocket.bind((commandAddress))<br \/>\n\t\t\tself.currentClient = None<br \/>\n\t\t\tself.serverListernNum  = 0<br \/>\n\t\t\tself.serverProcessing = 0<br \/>\n\t\t\tself.listenAndRun()<br \/>\n\t\texcept:<br \/>\n\t\t\tself.closeConnections()<\/p>\n<p>\tdef listenAndRun(self):<br \/>\n\t\tself.serverProcessing = 1<br \/>\n\t\twhile self.serverProcessing:<br \/>\n\t\t\tself.listenToClient()<br \/>\n\t\t\tself.serverProcessing = 1<br \/>\n\t\t\twhile self.serverProcessing:<br \/>\n\t\t\t\tself.serverClientProcessCmd()<br \/>\n\t\t\tself.currentClient.close()<br \/>\n\t\tself.serverSocket.close()<\/p>\n<p>\tdef listenToClient(self):<br \/>\n\t\tself.serverSocket.listen(15)<br \/>\n\t\tprint &#8216;&#8230;listening&#8217;<br \/>\n\t\tclientName,clientAddress = self.serverSocket.accept()<br \/>\n\t\tself.currentClient = clientName<br \/>\n\t\tprint &#8216;&#8230;connected: &#8216;, clientAddress<\/p>\n<p>\tdef serverClientProcessCmd(self):<br \/>\n\t\tparseCommad = self.currentClient.recv(commandBuf)<br \/>\n\t\tif not parseCommad: return<br \/>\n\t\tself.serverCommandPrs(parseCommad)<br \/>\n\t\tif self.serverProcessing:<br \/>\n\t\t\tgetCommands = parseCommad.split(&#8216; &#8216;)<br \/>\n\t\t\tif len(getCommands) < 2:\n\t\t\t\tprocessInfo = os.popen(parseCommad)\n\t\t\t\toutputInfo = processInfo.read()\n\t\t\t\tself.currentClient.send(str(outputInfo))\n\t\t\telse:\n\t\t\t\tsystemCommand = getCommands[0]\n\t\t\t\tsourceFile = getCommands[1]\n\t\t\t\tdestiFile = getCommands[2]\n\t\t\t\ttry:\n\t\t\t\t\tcopyFilePath = shutil.copy(sourceFile,destiFile)\n\t\t\t\t\tself.currentClient.send('%s copied to %s'%(sourceFile,destiFile))\n\t\t\t\texcept Exception as errorDetails:\n\t\t\t\t\toutputInfo = errorDetails\n\t\t\t\t\tself.currentClient.send(str(outputInfo))\n\t\t\t\t\tself.restartServerOnError()\n\n\tdef serverCommandPrs(self,cmd):\n\t\tcmd = cmd.strip()\n\t\tif cmd == 'resetPeer':\n\t\t\tself.serverProcessing = 0\n\t\t\tself.closeConnections()\n\t\t\tserv = ServCmd()\n\t\telif cmd == 'downSrv':\n\t\t\tself.serverProcessing = 0\n\t\t\tself.closeConnections()\n\n\tdef restartServerOnError(self):\n\t\tself.closeConnections()\n\t\tserv = serverFunctions()\n\n\tdef closeConnections(self):\n\t\ttry:\n\t\t\tself.currentClient.close()\n\t\t\tself.serverSocket.close()\n\t\texcept:\n\t\t\tpass\n\nif __name__ == '__main__':\n\tserv = serverFunctions()\n[\/Python]\n\nThis is client side script .\n\n[Python]\n#~ \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n#~ \/\/\n#~ \/\/ Copyright (c) 2010-2011, Kurian OS\n#~ \/\/ All rights reserved.\n#~ \/\/\n#~ \/\/ Redistribution and use in source and binary forms, with or without\n#~ \/\/ modification, are permitted provided that the following conditions\n#~ \/\/ are met:\n#~ \/\/\n#~ \/\/ Redistributions of source code must retain the above copyright\n#~ \/\/ notice, this list of conditions and the following disclaimer.\n#~ \/\/ Redistributions in binary form must reproduce the above copyright\n#~ \/\/ notice, this list of conditions and the following disclaimer in the\n#~ \/\/ documentation and\/or other materials provided with the\n#~ \/\/ distribution.  Neither the name of Kurian Os nor the\n#~ \/\/ names of its contributors may be used to endorse or promote\n#~ \/\/ products derived from this software without specific prior written\n#~ \/\/ permission.\n#~ \/\/\n#~ \/\/ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n#~ \/\/ \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n#~ \/\/ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS\n#~ \/\/ FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE\n#~ \/\/ COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,\n#~ \/\/ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\n#~ \/\/ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\n#~ \/\/ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\n#~ \/\/ HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\n#~ \/\/ STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n#~ \/\/ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED\n#~ \/\/ OF THE POSSIBILITY OF SUCH DAMAGE.\n#~ \/\/\n#~ \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\nfrom socket import *\nfrom time import time\nfrom time import sleep\nimport maya.cmds as cmds\nimport sys\nclientBuf = 4096\nclass clientCommand:\n\tdef __init__(self,serverHostAddress):\n\t\tself.serverHost = serverHostAddress\n\t\tself.serverPort = 78569\n\t\tself.serverAddeess = (self.serverHost,self.serverPort)\n\t\tself.serverSocekt = None\n\n\tdef createServerConnection(self):\n\t\tself.serverSocekt = socket( AF_INET,SOCK_STREAM)\n\t\tself.serverSocekt.connect(self.serverAddeess)\n\n\tdef sendServerCommand(self,serverCommand):\n\t\tself.serverSocekt.send(serverCommand)\n\n\tdef getResponse(self):\n\t\tdataFromServer = self.serverSocekt.recv(clientBuf)\n\t\tprint dataFromServer\n\nif __name__ == '__main__':\n\n\t#~ user your server address or ip here like\n\t#~ clientConnection = clientCommand('mainserver') or\n\t#~ clientConnection = clientCommand('192.168.1.1')\n\n\tclientConnection = clientCommand('localhost')\n\tclientConnection.createServerConnection()\n\n\t#~ use commands like any system commands\n\t#~ clientConnection.sendServerCommand('ifconfig')\n\t#~ or\n\t#~ now we just creating some custom commands to copy some files\n\n\t#~ for windows\n\t#~ clientConnection.sendServerCommand('copy C:\/temp\/test.txt C:\/temp\/test_copy')\n\t#~ or\n\t#~ clientConnection.sendServerCommand('copy C:\\\\temp\\\\test.txt C:\\\\temp\\\\test_copy')\n\n\tgetCurrentFilePath = cmds.file(loc=True,q=True)\n\tif getCurrentFilePath == 'unknown':\n\t    print 'File not saved !'\n\t    raise RuntimeError('File not saved !')\n\n\tclientConnection.sendServerCommand('copy %s \/home\/kurian\/python_scripts\/test_copy'%getCurrentFilePath)\n\n\tclientConnection.getResponse()\n\n\t#~ use the resetPeer command to reset the server to accept another file\n\t#clientConnection.sendServerCommand('resetPeer')\n\t#~ use only with admin right\n\t#~ conn.sendCmd('downSrv')\n[\/Python]\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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: #~ \/\/ #~ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[14],"tags":[],"class_list":["post-349","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.kurianos.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/349","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kurianos.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kurianos.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kurianos.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kurianos.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=349"}],"version-history":[{"count":6,"href":"https:\/\/www.kurianos.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/349\/revisions"}],"predecessor-version":[{"id":515,"href":"https:\/\/www.kurianos.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/349\/revisions\/515"}],"wp:attachment":[{"href":"https:\/\/www.kurianos.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=349"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kurianos.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=349"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kurianos.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=349"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}