2014-01-25 100 views
2

我的計算機上有2個網絡(具有互聯網的Wifi網絡,以及基於有線電纜的本地網絡),具有特定的掩碼和IP(此配置有效)。HTTP服務器Python:如何在本地測試它?

我對本地網絡的Python HTTP服務器(我的電腦上):

或者乾脆

>>> python server.py 
Serving on localhost:8000 

You can use this to test GET and POST methods. 

""" 

import SimpleHTTPServer 
import SocketServer 
import logging 
import cgi 

import sys 


if len(sys.argv) > 2: 
    PORT = int(sys.argv[2]) 
    I = sys.argv[1] 
elif len(sys.argv) > 1: 
    PORT = int(sys.argv[1]) 
    I = "" 
else: 
    PORT = 8000 
    I = "" 


class ServerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): 

    def do_GET(self): 
     print "======= GET STARTED =======" 
     logging.warning("======= GET STARTED =======") 
     logging.warning(self.headers) 
     SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self) 

    def do_POST(self): 
     print "======= POST STARTED =======" 
     logging.warning("======= POST STARTED =======") 
     logging.warning(self.headers) 
     form = cgi.FieldStorage(
      fp=self.rfile, 
      headers=self.headers, 
      environ={'REQUEST_METHOD':'POST', 
        'CONTENT_TYPE':self.headers['Content-Type'], 
        }) 
     logging.warning("======= POST VALUES =======") 
     for item in form.list: 
      logging.warning(item) 
     logging.warning("\n") 
     SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self) 

Handler = ServerHandler 

httpd = SocketServer.TCPServer(("10.0.0.2", PORT), Handler) 

print "@rochacbruno Python http server version 0.1 (for testing purposes only)" 
print "Serving at: http://%(interface)s:%(port)s" % dict(interface=I or "localhost", port=PORT) 
httpd.serve_forever() 

我只是想嘗試一下,如果這臺服務器的工作原理:從我的谷歌Chrome,我設置這個網址:10.0.0.2/?method=1234

無痕出現在Python的外殼...

所以,我的問題是:如何從我的電腦在網絡測試該服務器的Python rst(可以肯定它有效)?

+0

我已經跑你的代碼......它史詩般的失敗,並返回我** socket.error:[Errno 10049]請求的地址是無效的在其上下文**是否我更改端口從8000到888 。 – ZF007

+0

看到我解答它的答案。 – ZF007

回答

0

好吧,我發現問題:它是端口號;我把它改爲888,例如,它現在的作品:-)

安東尼

0

您的代碼在2017年引發以下錯誤:

socket.error: [Errno 10049] The requested address is not valid in its context

錯誤是由下面的代碼行造成的:

httpd = SocketServer.TCPServer(("10.0.0.2", PORT), Handler)

當IP「10.0.0.2」更改爲「127.0.0.1」時,無論您的端口更新8000 - > 888,它都會再次工作。享受!

相關問題