【问题标题】:Accessing python web server from external machine从外部机器访问 python web 服务器
【发布时间】:2016-01-17 15:05:28
【问题描述】:

我在 Ubuntu 上运行的 python Web 服务器的可见性/可访问性遇到问题。服务器代码如下:

#!/usr/bin/python
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer

PORT_NUMBER = 8899

#This class will handles any incoming request from
#the browser
class myHandler(BaseHTTPRequestHandler):

        #Handler for the GET requests
        def do_GET(self):
                self.send_response(200)
                self.send_header('Content-type','text/html')
                self.end_headers()
                # Send the html message
                self.wfile.write("Hello World !")
                return

try:
        #Create a web server and define the handler to manage the
        #incoming request
        server = HTTPServer(('', PORT_NUMBER), myHandler)
        print 'Started httpserver on port ' , PORT_NUMBER

        #Wait forever for incoming htto requests
        server.serve_forever()

except KeyboardInterrupt:
        print '^C received, shutting down the web server'
        server.socket.close()

使用 curl 和以下命令 在本地调用它有效 - 我收到“hello world”的答案。

curl {externalIP}:8899

在浏览器(chrome,ie)中打开地址失败!

http://{externalIP}:8899/

ufw 状态为非活动状态

iptables 如下

Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:8765

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

Ubuntu 已安装 apache2 服务器并使用 Web 浏览器打开 html 文件,外部 ip 和端口 80 从上面的服务器正常工作...

还有什么可以检查的吗?

【问题讨论】:

    标签: python ubuntu webserver


    【解决方案1】:

    我认为您可能正在收听环回接口,而不是连接到互联网的接口。

    要么指定IP,要么使用:

    server = HTTPServer(('0.0.0.0', PORT_NUMBER), myHandler)

    指定监听所有网络接口。

    【讨论】:

    • 嗨。谢谢您的回答。不幸的是,在指定的代码行中将第一个参数更改为外部 ip(并在第二次尝试为 0.0.0.0)后效果是相同的。本地 curl 有效,外部浏览器超时。
    • 对另一台机器是否正常工作?如果您从客户端远程登录到端口 8899,响应是什么?
    • 我使用 putty (ssh) 配置了所有内容,并且可以 ping ubuntu。更多的是配置了 apache2,我可以访问它以查看 html 目录中的 html 文档。我根本无法访问我的网络服务器。
    【解决方案2】:

    删除 apache 解决了这种情况。我不知道为什么,因为它应该阻塞端口 80,但是在此之后它现在可以工作了:

    apt-get remove apache2* 
    

    【讨论】:

      猜你喜欢
      • 2012-12-11
      • 2020-02-28
      • 1970-01-01
      • 1970-01-01
      • 2011-06-27
      • 1970-01-01
      • 2018-08-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多