Python calls the Powershell,flask framework to implement the RemoteApp interface

Source: Internet
Author: User


1, GET_REMOTEAPP.PS1

Set-executionpolicy remotesignedimport-module remotedesktopservicesfunction Getapp () {$result = Ls-l RDS:\RemoteApp\R Emoteappprograms $res = $result | Foreach-object {$_. Name} return $res}getapp

2, New_remoteapp.ps1

Set-executionpolicy remotesignedimport-module remotedesktopservices$applist= "C:\Program Files\ Premiumsoft\navicat premium\navicat.exe "," c:\program files  (x86) \jetbrains\pycharm  Community edition 2016.2.3\bin\pycharm.exe "," c:\program files\premiumsoft\navicat premium\ Navicat.exe "," c:\program files  (x86) \jetbrains\pycharm community edition 2016.2.3\bin \pycharm.exe "," c:\program files  (x86) \openvpn\bin\openvpn-gui.exe "," c:\program files  (x86) \mozilla firefox\firefox.exe "," c:\program files  (x86) \kugou\kgmusic\kugou.exe "," C:\Program  Files  (x86) \teamviewer\teamviewer.exe "," c:\program files  (x86) \youdao\youdaonote\ YoudaoNote.exe "," C:\Users\Administrator.WIN-403TF0V1RLC\AppData\Local\youdao\dict\Application\YodaoDict.exe "," "Function checkapppath ($app) {    foreach ($list  in  $appList) {         if  ($list  -like  "* $app *") {           $ index =  $list  | % {[array]::indexof ($appList, $_)}             $appPath  =  $appList [$index]        return  $appPath     }    }}function new-rdsremoteapp ($appName ) {     #返回值说明:     # 2 : program already exists     # 1  :  Add Success     # 0 :  add failure     if  (Test-path  rds:\remoteapp\remoteappprograms\ $appName)  {        return  2    }else {     $appPath  = checkapppath $ Appname        new-item -path rds:\remoteapp\ remoteappprograms -name  $appName  -applicationpath  $appPath  > $null         if   (test-path rds:\remoteapp\remoteappprograms\ $appName)  {             return 1        } else  {            return 0         }    }}New-RDSRemoteApp  $args [0]

3, Remove_remoteapp.ps1

set-executionpolicy remotesignedimport-module  Remotedesktopservicesfunction removeapp ($appName) {     #返回值0  : Software does not exist       #返回值1  :  Delete success      #返回值2  :  Delete failed     if   (-not  (test-path rds:\remoteapp\remoteappprograms\ $appName)) {         return 0    }else{    remove-item -path  rds:\remoteapp\remoteappprograms\ $appName  -recurse -force -confirm: $false  |  out-null    if  (-not  (test-path rds:\remoteapp\remoteappprograms\ $appName)) {        return 1    }else{         return 2    }         }}RemoveAPP  $args [0] 

4, callpowershell.py

Import subprocessimport jsondef newapp (param1):    try:         args = [r "PowerShell",  r "C:\flask_remoteAPP_http\PosershellModule \new_remoteapp.ps1 ", param1]        p = subprocess. Popen (args, stdout=subprocess. PIPE)         dt = p.stdout.read ()          return dt    except Exception, e:         print e    return falsedef delapp (param1):     try:        args = [r "PowerShell",  r "C:\flask_remoteAPP_http\PosershellModule\Remove_RemoteAPP.ps1",param1]         p = subprocess. Popen (args, stdout=subprocess. PIPE)         dt = p.stdout.read ()         return  dt    except Exception, e:         Print e    return falsedef getapp ():    try:         args = [r "PowerShell",  r "C:\flask_remoteAPP_http\ Posershellmodule\get_remoteapp.ps1 "]        p = subprocess. Popen (args, stdout=subprocess. PIPE)         dt = p.stdout.read ()          datalist = dt.split ("\ n")          Data = json.dumps (DataList)         return data     except exception, e:        print  E    return falsedef queryallapp ():     f1 = open ( R "C:\flask_remoteAPP_http\PosershellModule\appInventory.txt", "R")     data = {}     appdata = {}    for i in f1.readlines ():         a = i.decode ("GBK"). Split (",")          data[a[1]] = a[0]    appdata["All"] =  Data    jsondata = json.dumps (AppData)     return  Jsondata

5, run.py

From flask import flask,requestimport jsonfrom callpowershell import newapp , Delapp,getapp,queryallappapp=flask (__name__)    @app. Route ('/newapp ',  methods=[' GET ', ' POST ')   def newapps ():     try:        if  request.method ==  ' POST ':             jsondata = request.get_data ()              dictdata = json.loads (Jsondata)              response = newapp (dictdata["AppName")              return response                     else:             mes =  '                  <p>

6, client.py

#coding: Utf-8import urllib2import jsondef newapp (appName):     url =   ' Http://192.168.1.115:5000/newapp '      #url  =  ' http://192.168.1.115:5000/ DeLapp '     data = {' appName ': appname}    headers  = {' Content-type ':  ' Application/json '}    req = urllib2. Request (Url=url, headers=headers, data=json.dumps (data))     response =  urllib2.urlopen (req)     return response.read () Def delapp (appName):     url =  ' Http://192.168.1.115:5000/delapp '     data = { ' AppName ':  appname}    headers = {' content-type ':  ' Application/json '}     req = urllib2. Request (Url=url, headers=headers, data=json.dumps (data))     response&Nbsp;= urllib2.urlopen (req)     return response.read () def getApp ():     url =  ' Http://192.168.1.115:5000/getapp '     req =  Urllib2. Request (Url=url)     response = urllib2.urlopen (req)      Return response.read () Def queryallapp ():    url =  '/HTTP 192.168.1.115:5000/queryall '     req = urllib2. Request (Url=url)     response = urllib2.urlopen (req)      Return response.read () if __name__ ==  "__main__":    a =  Queryallapp ()     b  = json.loads (a)     print b

7. Interface description

1. Add the App interface
Request Method: POST
Transfer data type: JSON
Request Url:http://192.168.1.115:5000/newapp
Request parameter: {' appName ': program alias}
Return data type: string
return Result:
Return "1" to add success
Back to "2" program already exists
Return "0" Add failed

2. Remove the App interface
Request Method: POST
Transfer data type: JSON
Request Url:http://192.168.1.115:5000/delapp
Request parameter: {' appName ': program alias}
Return data type: string
return Result:
Return "1" Delete succeeded
Return "2" Delete failed
Back to "0" app does not exist

3. Get the list of apps you've added
Request Method: GET
Request Url:http://192.168.1.115:5000/getapp
Request parameters: No parameters
Return data type: JSON
Return data: [' App1 ', ' app2 ', ' app3 ']

4. Get a list of apps that can be added (including added)
Request Method: GET
Request Url:http://192.168.1.115:5000/getapp
Request parameters: No parameters
Return data type: JSON
Return data: {' All ': {' App1 alias ': ' App1 Chinese name ', ' app2 alias ': ' App2 Chinese name '}}

This article is from the "Q Group: 223843163" blog, please be sure to keep this source http://freshair.blog.51cto.com/8272891/1890352

Python calls the Powershell,flask framework to implement the RemoteApp interface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.