簡體   English   中英

從外部機器訪問python Web服務器

[英]Accessing python web server from external machine

我在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)失敗!

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在上面的服務器上正常工作...

有什么想法我還能檢查嗎?

我認為您可能正在監聽回送接口,而不是連接到互聯網的那個。

指定IP或使用:

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

指定監聽所有的網絡接口。

刪除阿帕奇解決了這種情況。 我不知道為什么,因為它應該阻塞端口80,但是在此之后它現在可以工作:

apt-get remove apache2* 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM