简体   繁体   English

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

[英]Accessing python web server from external machine

I am experiencing problems with visibility / accessibility of my python web server running on Ubuntu. 我在Ubuntu上运行的python Web服务器的可见性/可访问性遇到问题。 Server code is below: 服务器代码如下:

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

Calling it locally using curl with below command works - I receive answer with 'hello world'. 它调用本地使用curl下面的命令作品 -我收到的“Hello World”的答案。

curl {externalIP}:8899

Opening address in the browser (chrome, ie) fails! 在浏览器中打开地址(即Chrome)失败!

http://{externalIP}:8899/

ufw status is inactive ufw状态为无效

iptables as below 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 has apache2 server installed and opening html files using web browser, external ip and port 80 is working with no problem from above server... Ubuntu已安装apache2服务器并使用Web浏览器打开html文件,外部ip和端口80在上面的服务器上正常工作...

Any ideas what else could I check? 有什么想法我还能检查吗?

I think you might be listening on the loopback interface and not the one that is connected to the internet. 我认为您可能正在监听回送接口,而不是连接到互联网的那个。

Either specify IP or use: 指定IP或使用:

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

to specify to listen to all your network interfaces. 指定监听所有的网络接口。

Removing apache fixed this case. 删除阿帕奇解决了这种情况。 I do not know why, because it should block port 80, but it works now after this: 我不知道为什么,因为它应该阻塞端口80,但是在此之后它现在可以工作:

apt-get remove apache2* 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM