我总是用$ python -m SimpleHTTPServer快速的本地静态网页测试,它的伟大工程,index.htm或index.html为索引文件.
但是我需要使用default.htm或default.html正在进行我正在进行的项目.有人可以帮忙写一个简单的脚本吗?
我在网上找到了以下示例,我希望它可以帮助我们开始一点点.
import sys, SimpleHTTPServer, BaseHTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler
SimpleHTTPRequestHandler.protocol_version = "HTTP/1.0"
httpd = BaseHTTPServer.HTTPServer(('127.0.0.1', 8000), SimpleHTTPRequestHandler)
sa = httpd.socket.getsockname()
print "Serving HTTP on", sa[0], sa[1], "..."
httpd.serve_forever()
Run Code Online (Sandbox Code Playgroud) 我有以下代码:
import os
from ghost import Ghost
import urlparse, urllib
import SimpleHTTPServer
import SocketServer
import sys, traceback
from threading import Thread, Event
from time import sleep
please_die = Event() # this is my enemy
httpd = None
PORT = 8001
address = 'http://localhost:'+str(PORT)+'/'
search_dir = './category'
def main():
"""
basic run script routine,
FIXME: is supossed to exits gracefully
"""
thread = Thread(target = simpleServe)
try:
thread.start()
run()
except KeyboardInterrupt:
print "Shutdown requested"
except Exception:
traceback.print_exc(file=sys.stdout)
shutdown()
sys.exit(0)
def shutdown(): …Run Code Online (Sandbox Code Playgroud) 我正在python -m SimpleHTTPServer 8888从目录运行my_server,我想访问index.html包含读取的javascript命令my_file.csv的文件,但该文件不在my_server其子文件夹或其中一个子文件夹中.
path/to/my_server $ ln -s /some_path/to/my_file.csv symbolic_link_to_my_file.csv
path/to/my_server $ python -m SimpleHTTPServer 8888
Run Code Online (Sandbox Code Playgroud)
如果我创建符号链接里面my_server指向my_file.csv,然后访问它像这样:
http://localhost:8888/my_server/index.html?file=symbolic_link_to_my_file.csv
Run Code Online (Sandbox Code Playgroud)
有什么办法我可以用javascript里面的这个符号链接index.html来阅读my_file.csv?
<script>
//read URL params
...
d3.csv(file, ...)
</script>
Run Code Online (Sandbox Code Playgroud) 所以我通过这样做在端口 8000 上的 Ubuntu 机器上创建了一个简单的服务器:
python -m SimpleHTTPServer
10.127.11.18 - - [14/Aug/2014 15:11:55] "GET / HTTP/1.1" 200 -
10.127.11.18 - - [14/Aug/2014 15:11:55] code 404, message File not found
10.127.11.18 - - [14/Aug/2014 15:11:55] "GET /favicon.ico HTTP/1.1" 404 -
10.127.11.18 - - [14/Aug/2014 15:12:02] "GET /crazysean/ HTTP/1.1" 200 -
10.127.11.18 - - [14/Aug/2014 15:12:37] "GET /crazysean/ HTTP/1.1" 200 -
10.127.11.18 - - [14/Aug/2014 15:12:52] "GET /crazysean/?url=www.google.com&x=200&y=400 HTTP/1.1" 301 -
10.127.11.18 - - [14/Aug/2014 15:12:52] "GET /crazysean/?url=www.google.com&x=200&y=400/ HTTP/1.1" 200 -
10.127.11.18 …Run Code Online (Sandbox Code Playgroud) 我想用我的Java HttpServer进行一些SQL查询,但似乎HttpServer无法识别我提交到浏览器的链接中的特殊字符:
[1]: http://localhost:8001/test?query=SELECT * WHERE{ ?s rdf:type ?o. }
Run Code Online (Sandbox Code Playgroud)
我总是收到这样的回复:
400 Bad Request
URISyntaxException thrown
Run Code Online (Sandbox Code Playgroud)
这是我的服务器的代码:
public class SimpleHttpServer
{
public static void main(String[] args) throws Exception
{
dir = args[0];
HttpServer server = HttpServer.create(new InetSocketAddress(8001), 0);
server.createContext("/test", new MyHandler());
server.setExecutor(null);
server.start();
}
static class MyHandler implements HttpHandler {
public void handle(HttpExchange t) throws IOException {
String response ;
System.out.println(t.getRequestURI().getQuery().toString().replaceAll("query=", ""));
response = ExecuteHttpQuery.getInstance().httpQuery(t.getRequestURI().getQuery().toString().replaceAll("query=", "").toString(), dir) + "\n";
t.sendResponseHeaders(200, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
} …Run Code Online (Sandbox Code Playgroud)