Friday, 15 July 2011

javascript - Python - Detect browser closure to kill webserver -



javascript - Python - Detect browser closure to kill webserver -

ok, i'm writing pyqt software generate webpage. due security issues chrome , other things, need webserver test webpage.

so thought create button called run, can click or press f5 start server, , open browser page. snippet of code button calls, simplified (there code things, including changing current directory , such), looks this:

import sys import webbrowser simplehttpserver import simplehttprequesthandler handlerclass basehttpserver import httpserver serverclass protocol = 'http/1.0' port = 8080 ip = '127.0.0.1' new = 2 #goes new tab url = "http://"+ip+":{0}".format(port) serveraddress = (ip,port) handlerclass.protocol = protocol httpd = serverclass(serveraddress, handlerclass) sa = httpd.socket.getsockname() webbrowser.open(url,new=new) httpd.serve_forever()

ok, problem serve_forever called, can expected serve forever. there way kill server after browser closed?

edit: understand many people recommend using threads can't find way observe browser has closed or killing thread in scheme monitor (i'm on ubuntu) while testing.

edit2: ok, i've read webbrowser.py, doesn't seem homecoming sort of process identifier...

edit3: i'm reflecting on this, maybe right approach checking if accessing server, , if not, kill it... way can observe if tab closed... problem way can think uses dummy page powerfulness loads whatever page test inside, seems hackish...

it seems if can find way of doing this, maybe through error responses...i can webserver in subprocess has while , exits 1 here: https://docs.python.org/2/library/basehttpserver.html#more-examples

import sys #from threading import thread import webbrowser import basehttpserver import simplehttpserver serverclass=basehttpserver.httpserver handlerclass=simplehttpserver.simplehttprequesthandler protocol = "http/1.0" port = 8080 ip = '127.0.0.1' new = 2 #2 goes new tab, 0 same , 1 window. url = "http://"+ip+":{0}".format(port) handlerclass.protocol = protocol httpd = serverclass((ip,port), handlerclass) sa = httpd.socket.getsockname() print("\n---\nserving http on {0}, port {1}\n---\n".format(sa[0],sa[1]) ) browserok = webbrowser.open(url,new=new) def runwhiletrue(): while true: #print(vars(httpd)) httpd.handle_request() runwhiletrue()

right i'm thinking using timer watchdog, if server not used more period, get's killed... think awful solution... wanted browser ping for time while tab opened...maybe, don't know if optimal, looking code right : simplehttpserver , socketserver .

thinking maybe if server understand message website break loop. tab closure detected in javascript here : browser/tab close detection using javascript (or other language). don't know how communicate server.

editfinal:

in javascript code of webpage, i've inserted:

window.addeventlistener('unload', function (e) { e.preventdefault(); jsonlevelget("http://127.0.0.1:8081/exit.json"); }, false);

then, python code server.py:

import sys threading import thread import webbrowser import basehttpserver import simplehttpserver serverclass=basehttpserver.httpserver handlerclass=simplehttpserver.simplehttprequesthandler protocol = "http/1.0" port = 8080 ip = '127.0.0.1' admip = ip admport = 8081 new = 2 #2 goes new tab, 0 same , 1 window. url = "http://"+ip+":{0}".format(port) handlerclass.protocol = protocol httpdgame = serverclass((ip,port), handlerclass) httpdadm = serverclass((admip,admport), handlerclass) sa = httpdgame.socket.getsockname() sb = httpdadm.socket.getsockname() print("\n---\nserving http on {0}, port {1}\n---\n".format(sa[0],sa[1]) ) print("\n---\nadm http listening on {0}, port {1}\n---\n".format(sb[0],sb[1]) ) browserok = webbrowser.open(url,new=new) def rungameserver(): httpdgame.serve_forever() print("\nrungameserver stopped\n") httpdadm.shutdown() homecoming def runadmserver(): httpdadm.handle_request() httpdgame.shutdown() print("\nrunadmserver stopped\n") homecoming gameserverthread = thread(target=rungameserver) gameserverthread.daemon = true admserverthread = thread(target=runadmserver) gameserverthread.start() admserverthread.start() admserverthread.join()

it works! when tab closed, server.py code exits! @st.never!

as said, observe (in javascript, in browser) window beingness closed, , send 1 lastly request server shut down.

if don't want inspect requests searching "poweroff request", can instead have server hear on 2 different ports (probably on different threads). example, "main" server listens on port 8080 current behaviour, , separate instance listens on port 8081. can shut downwards server whenever request reaches port 8081.

javascript python browser httpserver

No comments:

Post a Comment