Python webserver vereinfacht.
This commit is contained in:
parent
3e5e9fec26
commit
83af324d99
|
@ -1,41 +1,11 @@
|
||||||
[[Category:Ruby]] [[Category:Python]]
|
[[Category:Ruby]] [[Category:Python]]
|
||||||
=Python=
|
=Python2=
|
||||||
==beginner version==
|
<source lang="bash">
|
||||||
<source lang="python">
|
python -m SimpleHTTPServer 8888
|
||||||
#!/usr/bin/env python
|
|
||||||
import sys
|
|
||||||
import os
|
|
||||||
from SocketServer import ThreadingMixIn
|
|
||||||
from BaseHTTPServer import HTTPServer
|
|
||||||
from SimpleHTTPServer import SimpleHTTPRequestHandler
|
|
||||||
class ThreadingHTTPServer(ThreadingMixIn, HTTPServer):
|
|
||||||
pass
|
|
||||||
if len(sys.argv) != 3:
|
|
||||||
print "Usage: %s <port> <dir>" % (sys.argv[0],)
|
|
||||||
sys.exit()
|
|
||||||
address = ('', int(sys.argv[1]))
|
|
||||||
server = ThreadedHTTPServer(address, SimpleHTTPRequestHandler)
|
|
||||||
os.chdir(sys.argv[2])
|
|
||||||
try:
|
|
||||||
server.serve_forever()
|
|
||||||
except KeyboardInterrupt:
|
|
||||||
pass
|
|
||||||
</source>
|
</source>
|
||||||
==advanced version==
|
=Python3=
|
||||||
<source lang="python">
|
<source lang="bash">
|
||||||
#!/usr/bin/env python
|
python3 -m http.server 8888
|
||||||
import sys
|
|
||||||
from twisted.python import log
|
|
||||||
from twisted.internet import reactor
|
|
||||||
from twisted.web import server, static
|
|
||||||
if len(sys.argv) != 3:
|
|
||||||
print "Usage: %s <port> <dir>" % (sys.argv[0],)
|
|
||||||
sys.exit()
|
|
||||||
root = static.File(sys.argv[2])
|
|
||||||
site = server.Site(root)
|
|
||||||
reactor.listenTCP(int(sys.argv[1]), site)
|
|
||||||
log.startLogging(sys.stderr)
|
|
||||||
reactor.run()
|
|
||||||
</source>
|
</source>
|
||||||
==guru version==
|
==guru version==
|
||||||
<source lang="bash">
|
<source lang="bash">
|
||||||
|
|
Loading…
Reference in New Issue
Block a user