0

cherrypy 서버가 시작될 때 셀렌 모듈을 사용하여 브라우저를 열려고했습니다.응답 성 Cherrypy 개발 환경을 위해 selenium.webdriver 사용

cherrypy.Autoreload로 페이지를 새로 고침하여 마우스를 사용하지 않아도됩니다.

cherrypy 플러그인으로 너무 일찍 시작하면 서버가 응답하지 않고 세션을 종료하는 동안 오류가 발생합니다.

after_server_start 이벤트가 필요합니다. 모든 조언 ??

답변

0

http://docs.cherrypy.org/en/latest/extend.html#engine-as-a-pubsub-bus 그래서 나는 약간이 알아 냈어.

import cpideplugin 
class root:pass 

cherrypy.tree.mount(root(),"/") 

cpideplugin.CpIdePlugin(cherrypy.engine).subscribe() 
cherrypy.engine.start()#the pubsub engine 
cherrypy.server.start()#an html server channel on the engine 
cherrypy.engine.block() 
cpideplugin.command('close') 

이를 :

import cherrypy,time,os 
from cherrypy.process import wspbus, plugins 
import requests as r 


def command(method): 
    addr='http://127.0.0.1:3131/' 
    r.get(addr+method) 

class CpIdePlugin(plugins.SimplePlugin): 
    def __init__(self,bus): 
     plugins.SimplePlugin.__init__(self,bus) 
     self.bus.log('Init plug') 
     DriveId=os.getenv('CherryDrive') 
     self.running=False 
     if DriveId: 
      self.running=True 
     else: 
      command('open') 
      os.putenv('CherryDrive','True') 


    def start(self): 
     self.bus.log('Running FF plugin') 
     if self.running: 
      self.refresh() 
     else: 
      command('start') 

    def refresh(self):#need to make a channel i think 
     self.bus.log("Reload via bus") 
     command('refresh') 

그것은 내 주요 애플 리케이션에 묶어 :

from selenium import webdriver 
import cherrypy,time,os,signal 

class CpIde(): 
    def __init__(self):pass 

    @cherrypy.expose 
    def index(self):return "hello there" 

    @cherrypy.expose 
    def open(self): 
     if hasattr(self,'driver'): 
      try:self.driver.quit() 
      except: 
       self.driver.service.process.send_signal(signal.SIGTERM) 
     self.driver=webdriver.Firefox() 
     return "Opening FF" 

    @cherrypy.expose 
    def start(self): 
     try: 
      self.driver.get('http://127.0.0.1:8080') 
     except:pass 
     finally: 
      self.driver.execute_script(""" 
      document.body.innerHTML="Just one second"; 
      window.setTimeout(function(){window.location.reload(false)},300) """ 
) 
     return "starting FF" 

    @cherrypy.expose 
    def close(self): 
     self.driver.quit() 
     del self.driver 
     return 'Closing FF' 

    @cherrypy.expose 
    def refresh(self): 
     self.driver.execute_script(""" 
      document.body.innerHTML="Just one second"; 
      window.setTimeout(function(){window.location.reload(false)},300) """) 
     return "restarting" 

cherrypy.tree.mount(CpIde(),'/') 

그런 다음 내가 그것을 요청을 만드는 플러그인을 만들어 : 나는 cherryd를 사용하여 데몬 서버를 만들어 방법 셀레늄 드라이버는 Autoreload 새로 고치지 않고 나는 정력에서 자유롭게 일할 수 있습니다. 이것은 완벽한 코드가 아니며 모든 권장 사항에 대해 감사하겠습니다. 건배

0

cherrypy 엔진 (버스)의 start 단계를 사용할 수 있습니다. 자세한 내용은

import webbrowser                

import cherrypy                 
from cherrypy.process.plugins import SimplePlugin        

class Root:                  

    @cherrypy.expose                
    def default(self):               
     return "Hello World!"             

class OpenBrowser(SimplePlugin):             

    def start(self):                
     self.bus.log("Opening browser")           
     webbrowser.open('http://localhost:8080/')        

OpenBrowser(cherrypy.engine).subscribe()           
cherrypy.quickstart(Root()) 

는 :

+0

잘 작동하지만 모든 편집시 탭이 열립니다. 결국 나는 그것을 닫아야하거나 일상적으로해야합니다. –

+0

예,'webbrowser' 부분이 데모였습니다. 다시 시작했을 때 뭔가를 실행하는 방법에 중점을 두었습니다. 브라우저를 새로 고치려면 Selenium 코드를 구현해야합니다 (웹 드라이브로 생성 된 코드). – cyraxjoe