我正在努力建立一个简单的hello世界。我正在使用python运行服务器并将我的vagrant文件发送到localhost:8080到vagrant服务器8080,它输出一个简单的HTML hello消息。当我在本地计算机上键入Localhost:8080但是连接被拒绝。这是vagrant文件和我的python Web服务器代码。
Vagrant文件:
# -*- mode: ruby -*-
# vi: set ft=ruby :
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.provision "shell", path: "pg_config.sh"
# config.vm.box = "hashicorp/precise32"
config.vm.box = "ubuntu/trusty32"
config.vm.network "forwarded_port", guest: 8000, host: 8000
config.vm.network "forwarded_port", guest: 8080, host: 8080, autocorrect: true
config.vm.network "forwarded_port", guest: 5000, host: 5000
end
Webserver.py
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
class WebServerHandler(BaseHTTPRequestHandler):
def do_GET(self):
if self.path.endswith("/hello"):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
output = ""
output += "<html><body>Hello!</body></html>"
self.wfile.write(output)
print output
return
else:
self.send_error(404, "File Not Found %s" % self.path)
def main():
try:
port = 8080
server = HTTPServer(('',port), WebServerHandler)
print "Web Server is now running on port %s" % port
server.serve_forever() #constantly runs unless keyboard interrupted
except KeyboardInterrupt:
print "^c entered, stopping web server..."
server.socket.close()
if __name__ == '__main__':
main()