Z
Z
zelsky2015-01-14 01:03:59
linux
zelsky, 2015-01-14 01:03:59

Sending commands through the web interface?

The code below is written in the Go language, I need something similar but in Python. Tick ​​your nose what to pick?
And this example does not work for me = (
The author of this miracle HaCk3Dq is a github user. P.S.
I just know here with trouble links

package main

import (
  "io"
  "net/http"
  "os/exec"
  "strings"
)

var list = map[string]string{
  "+":     "amixer set Master playback 5%+",
  "-":     "amixer set Master playback 5%-",
  "next":  "foobnix --next",
  "prev":  "foobnix --prev",
  "pause": "foobnix --pause",
  "play":  "foobnix --play",
}

func get_cmd(w http.ResponseWriter, r *http.Request) {
  io.WriteString(w, `<html>
    <head>
      <style type='text/css'>
      body {
        zoom: 200%;
      }
      </style>
      <title>Remote control</title>
    </head> 
  <body>
    <form method="POST" action="">
      Enter command: <br>
      <input type="text" autofocus name="cmd" placeholder="">
      <button>Send</button>
    </form>
  </body>
  </html>`)

  run(strings.TrimSpace(r.FormValue("cmd")))
}

func run(cmd string) {
  if len(cmd) != 0 {
    if value, ok := list[cmd]; ok {
      execute(value)
      print("Executed: " + value + "\n")
    }
  }
}

func execute(cmd string) {
  parts := strings.Fields(cmd)
  head := parts[0]
  parts = parts[1:len(parts)]
  out, _ := exec.Command(head, parts...).Output()
  print(string(out))
}

func main() {
  print("RemoteServer started \n")
  http.HandleFunc("/linux", get_cmd)
  http.ListenAndServe(":1337", nil)
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
throughtheether, 2015-01-14
@throughtheether

Tick ​​your nose what to pick?
For example, bottle.py as a web server, subprocess for executing commands. If you want to get by with the standard library, then SimpleHTTPServer / BaseHTTPServer in the case of python2, http.server in the case of python3.

S
SilentFl, 2015-01-14
@SilentFl

it is a simple web server that allows you to execute some Linux commands remotely. Trouble something in what? What points in this code are not clear? Or is there a problem with python?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question