它使用的是內置在JDK6的HttpServer類。隨意提出改進建議,我是斯卡拉新手。
package org.test.simplehttpserver
import java.net.InetSocketAddress
import com.sun.net.httpserver.{HttpExchange, HttpHandler, HttpServer}
import collection.mutable.HashMap
abstract class SimpleHttpServerBase(val socketAddress: String = "127.0.0.1",
val port: Int = 8080,
val backlog: Int = 0) extends HttpHandler {
private val address = new InetSocketAddress(socketAddress, port)
private val server = HttpServer.create(address, backlog)
server.createContext("/", this)
def redirect(url: String) =
<html>
<head>
<meta http-equiv="Refresh" content={"0," + url}/>
</head>
<body>
You are being redirected to:
<a href={url}>
{url}
</a>
</body>
</html>
def respond(exchange: HttpExchange, code: Int = 200, body: String = "") {
val bytes = body.getBytes
exchange.sendResponseHeaders(code, bytes.size)
exchange.getResponseBody.write(bytes)
exchange.getResponseBody.write("\r\n\r\n".getBytes)
exchange.getResponseBody.close()
exchange.close()
}
def start() = server.start()
def stop(delay: Int = 1) = server.stop(delay)
}
abstract class SimpleHttpServer extends SimpleHttpServerBase {
private val mappings = new HashMap[String,() => Any]
def get(path: String)(action: => Any) = mappings += path -> (() => action)
def handle(exchange: HttpExchange) = mappings.get(exchange.getRequestURI.getPath) match {
case None => respond(exchange, 404)
case Some(action) => try {
respond(exchange, 200, action().toString)
} catch {
case ex: Exception => respond(exchange, 500, ex.toString)
}
}
}
class HelloApp extends SimpleHttpServer {
var count = 0
get("/") {
"There's nothing here"
}
get("/hello") {
"Hello, world!"
}
get("/markup") {
<html>
<head>
<title>Test Title</title>
</head>
<body>
Test Body
</body>
</html>
}
def countPage = <html>
<head>
<title>Test Title</title>
</head>
<body>
Count:
{count}<a href="/increaseCount">++</a>
<a href="/decreaseCount">--</a>
<a href="/resetCount">Reset</a>
</body>
</html>
get("/count") {
countPage
}
get("/resetCount") {
count = 0
redirect("/count")
}
get("/increaseCount") {
count = count + 1
redirect("/count")
}
get("/decreaseCount") {
count = count - 1
redirect("/count")
}
get("/error") {
throw new RuntimeException("Bad bad error occurred")
}
}
object Main {
def main(args: Array[String]) {
val server = new HelloApp()
server.start()
}
}
你的意思是在那裏apt-get的存在系統上的4個命令,easy_install的捆綁Python和使用sh的外殼。你是否也假設有root權限? – huynhjl 2010-11-12 16:25:31
是的,我還假設系統有CPU,RAM,存儲器,已安裝的操作系統,鍵盤和顯示器;) – Matthew 2010-11-12 17:10:04
這是一個嚴重的問題,或者只是試圖展示Python + Flask有多酷?在需要輸入15個命令而不是4個命令之前,需要多少次設置Web服務器纔是工作中的限速步驟?我承認這是非常酷的,但是我唯一可以從需要的地方開始思考的應用程序,而不是僅僅有趣的是將漏洞嵌入到一個小緩衝區中,該緩衝區將目標機器轉換爲Web服務器。 – 2010-11-12 21:45:24