从外部机器访问python Web服务器

时间:2016-01-17 15:05:28

标签: python ubuntu webserver

我遇到了在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正在使用上面的服务器没问题...

任何想法我还能检查什么?

2 个答案:

答案 0 :(得分:0)

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

指定IP或使用:

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

指定侦听所有网络接口。

答案 1 :(得分:0)

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

apt-get remove apache2*