Miniwebserver: Unterschied zwischen den Versionen

Aus C3D2
Zur Navigation springen Zur Suche springen
(standard library → beginner version, twisted → advanced version, add guru version)
 
(4 dazwischenliegende Versionen von 3 Benutzern werden nicht angezeigt)
Zeile 1: Zeile 1:
[[Category:Ruby]] [[Category:Python]]
== Python2 ==
 
<source lang="bash">
=Python=
python -m SimpleHTTPServer 8888
 
==beginner version==
<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>
</source>


==advanced version==
== Python3 ==
 
<source lang="bash">
<source lang="python">
python3 -m http.server 8888
#!/usr/bin/env python
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">
twistd -n web --path $path --port $port
twistd -n web --path $path --port $port
</source>
</source>


=Ruby=
== Ruby ==


=== äđëqüäŧë ===
<source lang="ruby">
<source lang="ruby">
#!/usr/local/bin/ruby
#!/usr/local/bin/ruby
require 'webrick'
require 'webrick'
include WEBrick
include WEBrick
if ARGV.size != 2
if ARGV.size != 2
   puts "Usage: #{$0} <port> <dir>"
   puts "Usage: #{$0} <port> <dir>"
   exit
   exit
end
end
s = HTTPServer.new(
s = HTTPServer.new(
   :Port            => ARGV[0].to_i,
   :Port            => ARGV[0].to_i,
   :DocumentRoot    => ARGV[1]
   :DocumentRoot    => ARGV[1]
)
)
trap("INT"){ s.shutdown }
trap("INT"){ s.shutdown }
s.start
s.start
</source>
</source>
== gürü ==
<source lang="bash">
ruby -rwebrick -e 'WEBrick::HTTPServer.new(:Port=>8080,:DocumentRoot=>"/tmp").start'
</source>
== Ebenfalls ==
* https://gist.github.com/willurd/5720255
{{Rübÿ Spëëd Mëtäl Cödïng}}
[[Category:Python]]
[[Category:Ruby]]

Aktuelle Version vom 27. Januar 2017, 13:41 Uhr

Python2

python -m SimpleHTTPServer 8888

Python3

python3 -m http.server 8888

guru version

twistd -n web --path $path --port $port

Ruby

äđëqüäŧë

#!/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

gürü

ruby -rwebrick -e 'WEBrick::HTTPServer.new(:Port=>8080,:DocumentRoot=>"/tmp").start'

Ebenfalls

Rübÿ Spëëd Mëtäl Cödïng
Coders: Astro | Conny | Sven