2011-11-18 23 views
0
class WebServer(SocketServer.ThreadingMixIn,BaseHTTPServer.HTTPServer): 

    # Works with basehttphandler 
    do_get(self): 
     if 'home' in self.path: 
      <do something here> 

# "Working" Method is commented out. The problem I'm having is being unable to 
# handle requests like GET, POST, etc with CGIHTTPRequestHandler.: 
# 
#DoIT=BaseHTTPServer.BaseHTTPRequestHandler((SERVER_ADDRESS,PORT),WebServer) 

DoIt=webserver((SERVER_ADDRESS,PORT),CGIHTTPServer.CGIHTTPRequestHandler) 
DoIT.serve_forever() 

這適用於basehttprequesthandler,而不是cgihttprequesthandler。如果可行的話,我需要一種方法來管理'類型'(使用兩個庫?)的請求。提前致謝。python basehttpserver和cgihttpserver do_GET(self),使用cgi requesthandler和base requesthandler

+0

當事情不工作,試圖給出一些細節。你有錯誤信息嗎?它沒有做你期望的事嗎? –

+0

本質上我想要做的是管理GET請求和其他HTTP請求方法。使用BaseHTTPServer,「do_GET(self)」起作用。只要我使用不再是選項的CGIHTTPRequestHandler。我沒有收到任何錯誤,它只是停止工作。因爲我需要能夠使用cgi,所以只使用basehttpserver不是項目可擴展的。基本上,我需要能夠使用CGIHTTPServer處理「do_GET(self)」類型的函數。對於任何混淆抱歉。解釋不太好。 – user1054340

回答

0

嘗試這樣:

import BaseHTTPServer 
import CGIHTTPServer 

server = BaseHTTPServer.HTTPServer 
server_address = ("", 8888) 

class MyHandler(CGIHTTPServer.CGIHTTPRequestHandler): 
    def do_GET(self): 
     #do something 

    def do_POST(self): 
     #do something 

httpd = server(server_address, MyHandler) 
httpd.serve_forever() 
相關問題