2011-11-07 27 views
0

即時通訊嘗試讓Python加載模塊,這取決於從瀏覽器請求什麼頁面,如果.py文件位於腳本的根目錄中,但它能夠工作讓它在一個子目錄中工作。Python - 在Python Web服務器中加載模塊

html文件可以在任何地方工作,如果有人可以幫助我讓python文件在任何目錄下工作,我會感激它。

IM上的Win 7 SP1 64位

import string,cgi,time 
from os import curdir, sep 
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer 
import os 
import mimetypes 

#import pri 
port = 888 
host = "0.0.0.0" 

class MyHandler(BaseHTTPRequestHandler): 

def do_GET(self): 
    try: 
     #RequestedURL = self.path 
     mimeType = mimetypes.guess_type(self.path)[0] 
     fileType = mimetypes.guess_extension(mimeType) 
     infoList = [mimeType, fileType] 

     if infoList[1] != ".py": 
      self.send_response(200) 
      self.send_header('Content-type', mimeType) 
      self.end_headers() 
      f = open(curdir + sep + self.path, "rb") 
      self.wfile.write(f.read()) 
      f.close() 
      return 

     if fileType == ".py": 
      pythonFilename = self.path.lstrip("/") 
      self.send_response(200) 
      self.send_header('Content-type', 'text/html') 
      self.end_headers() 
      pyname = pythonFilename.replace("/", ".")[:-3] 
      print pythonFilename 
      print pyname 
      temp1 = pyname.split(".") 
      temp2 = temp1[-1] 
      print temp2 
      module = __import__(root.index) 
      self.wfile.write(module.root.index.do_work()) 
      #module = __import__("test.index") 
      #self.wfile.write(module.index.do_work()) 
      return 

     return 

    except IOError: 
     self.send_error(404,'File Not Found: %s' % self.path) 


def do_POST(self): 
    global rootnode 
    try: 
     ctype, pdict = cgi.parse_header(self.headers.getheader('content-type')) 
     if ctype == 'multipart/form-data': 
      query=cgi.parse_multipart(self.rfile, pdict) 
     self.send_response(301) 

     self.end_headers() 
     upfilecontent = query.get('upfile') 
     print "filecontent", upfilecontent[0] 
     self.wfile.write("<HTML>POST OK.<BR><BR>"); 
     self.wfile.write(upfilecontent[0]); 

    except : 
     pass 

def main(): 
try: 
    server = HTTPServer((host, port), MyHandler) 
    print 'started httpserver:' 
    print ("Host: " + (host)) 
    print ("Port: " + str(port)) 

    server.serve_forever() 
except KeyboardInterrupt: 
    print '^C received, shutting down server' 
    server.socket.close() 

if __name__ == '__main__': 
main() 

回答

2

做的第一件事就是運行的Python 2.7是確保子目錄中有一個名爲

__init__.py 

文件。這個文件可以是空白的,但是如果沒有它,你將無法進行導入。然後,你應該知道,如果你是從一個子目錄中導入,你必須包括像

module = __import__("subdirectory.jack2") 

也就是說,你需要包括子目錄模塊的名稱存儲在導入模塊時。

如果你想從一個不是你工作目錄的子目錄的目錄導入,你必須確保它包含在python路徑中。你可以簡單地通過導入sys模塊設置sys.path,並追加到哪裏都存儲在模塊的新路徑修改路徑:

import sys 
sys.path.append("full/path/to/directory/to/import/from") 

編輯響應評論:

當使用builtin function __import__,重要的是要認識到它返回頂級模塊,而不是您想要導入的模塊。所以,如果你想訪問你剛纔導入的模塊,你需要做這樣的事情:

module = __import__("mymodule") 
result = module.mymodule.myfunction() 
+0

謝謝,我試過了,我得到這個錯誤 –

+0

文件「C:\ Python27 \ LIB \ SocketServer.py「,第284行,在_handle_request_noblock中 self.process_request(request,client_address) –

+0

實際錯誤是什麼?這只是錯誤的位置。 – Wilduck