Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Vagrant-本地主机连接被拒绝_Python_Webserver_Vagrant - Fatal编程技术网

Python Vagrant-本地主机连接被拒绝

Python Vagrant-本地主机连接被拒绝,python,webserver,vagrant,Python,Webserver,Vagrant,我正在尝试建立一个简单的hello世界。我正在运行一个使用python的服务器,并将我的vagrant文件转发给vagrant服务器8080,该服务器将输出一条简单的HTML hello消息。当我在本地计算机上键入Localhost:8080时,连接被拒绝。这是vagrant文件和我的python web服务器代码 流浪者档案: # -*- mode: ruby -*- # vi: set ft=ruby : VAGRANTFILE_API_VERSION = "2" Vagrant.con

我正在尝试建立一个简单的hello世界。我正在运行一个使用python的服务器,并将我的vagrant文件转发给vagrant服务器8080,该服务器将输出一条简单的HTML hello消息。当我在本地计算机上键入Localhost:8080时,连接被拒绝。这是vagrant文件和我的python web服务器代码

流浪者档案:

# -*- 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()
从BaseHTTPServer导入BaseHTTPRequestHandler,HTTPServer
类WebServerHandler(BaseHTTPRequestHandler):
def do_获得(自我):
如果self.path.endswith(“/hello”):
自我发送_响应(200)
self.send_标题('Content-type','text/html')
self.end_头()
output=“”
输出+=“你好!”
self.wfile.write(输出)
打印输出
返回
其他:
self.send_错误(404,“找不到文件%s”%self.path)
def main():
尝试:
端口=8080
服务器=HTTPServer(“”,端口),WebServerHandler)
打印“Web服务器现在正在端口%s上运行”%port
server.serve_forever()#持续运行,除非键盘中断
除键盘中断外:
打印“^c已输入,正在停止web服务器…”
server.socket.close()
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
main()

尝试在vagrant guest主机上运行
python-m SimpleHTTPServer 8000
。然后尝试访问它而不是脚本。这样,您可以确保它不是来自VirtualBox的网络错误。您可以使用任何ol的web浏览器浏览到它吗?您是否也检查了系统上是否有防火墙?我检查了#1,但仍然无法通过localhost:8000访问它。假设这是一个网络问题,我该怎么办?仅供参考。没有防火墙。我还尝试在本地终端上运行webserver.py,但仍然无法连接到本地主机:8080I解决了这个问题。我需要输入而不是本地主机:8080/helloTry在vagrant guest主机上运行
python-m SimpleHTTPServer 8000
。然后尝试访问它而不是脚本。这样你可以确保它不是来自VirtualBox的网络错误。你可以使用任何ol'web浏览器浏览到它吗?您是否也检查了系统上是否有防火墙?我检查了#1,但仍然无法通过localhost:8000访问它。假设这是一个网络问题,我该怎么办?仅供参考。没有防火墙。我还尝试在本地终端上运行webserver.py,但仍然无法连接到本地主机:8080I解决了这个问题。我需要输入而不是localhost:8080/hello