c3d2-wiki/Miniwebserver.mw

69 lines
1.6 KiB
Plaintext
Raw Normal View History

2007-11-14 21:26:39 +01:00
[[Category:Ruby]] [[Category:Python]]
=Python=
==beginner version==
2007-11-14 21:47:37 +01:00
<source lang="python">
#!/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>
==advanced version==
2007-11-14 21:26:39 +01:00
<source lang="python">
#!/usr/bin/env python
import sys
2007-11-14 21:29:42 +01:00
from twisted.python import log
2007-11-14 21:26:39 +01:00
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)
2007-11-14 21:29:42 +01:00
log.startLogging(sys.stderr)
2007-11-14 21:26:39 +01:00
reactor.run()
</source>
==guru version==
<source lang="bash">
twistd -n web --path $path --port $port
</source>
2007-11-14 21:26:39 +01:00
=Ruby=
2007-11-16 00:03:33 +01:00
==äđëqüäŧë==
2007-11-14 00:19:31 +01:00
<source lang="ruby">
#!/usr/local/bin/ruby
require 'webrick'
include WEBrick
if ARGV.size != 2
puts "Usage: #{$0} <port> <dir>"
exit
end
s = HTTPServer.new(
:Port => ARGV[0].to_i,
:DocumentRoot => ARGV[1]
)
trap("INT"){ s.shutdown }
s.start
</source>
2007-11-16 00:03:33 +01:00
==gürü==
<source lang="bash">
ruby -rwebrick -e 'WEBrick::HTTPServer.new(:Port=>8080,:DocumentRoot=>"/tmp").start'
</source>
{{Rübÿ Spëëd Mëtäl Cödïng}}