<p dir="ltr">To run python code embedded in a project file you have to use project macros. <br>
<br>
Have a look at this answer:<br>
<a href="http://gis.stackexchange.com/questions/89903/is-there-a-way-to-run-a-python-script-on-opening-a-qgis-project">http://gis.stackexchange.com/questions/89903/is-there-a-way-to-run-a-python-script-on-opening-a-qgis-project</a></p>
<p dir="ltr">Giovanni </p>
<div class="gmail_quot<blockquote class=" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><p dir="ltr">THX Giovanni forma tour precious help.<br>
I cannot use a file in .qgis2 folder. Because this mean to have a setup phase before to open the project. But i hope to apply the code directly inside the qgis project. Your code is really interesting.<br>
Many thx,<br>
Andrea</p>
<div class="gmail_quote">Il 24/apr/2014 13:55 "G. Allegri" <<a href="mailto:giohappy@gmail.com" target="_blank">giohappy@gmail.com</a>> ha scritto:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr"><div>You can write a startup.py file and put it into the <user>/.qgis2/python folder.</div><div>It will be loaded and executed, so you could check for internet availability with something as simple as:</div>
<div><br></div><div>------------------------------------------------------</div><div>import urllib2</div><div>url = <your_wms_server_getcapabilities_service></div><div>try:</div><div> urllib2.urlopen(url)</div><div>
except urllib2.URLError, e:</div><div> print "Could not connect"</div><div>-----------------------------------------------------</div><div><br></div><div>Last time I needed it I used the following (with various adaptations):</div>
<div><br></div><div>-----------------------------------------------------</div><div>import sys</div><div>import socket</div><div>import time</div><div><br></div><div>host = <my_url></div><div>port = <my_port_number></div>
<div># type => ["tcp" or "udp"]</div><div>type = sys.argv[3]</div><div>test = ""</div><div>if len(sys.argv) > 4 :</div><div> test = sys.argv[4]</div><div><br></div><div>while 1 :</div>
<div>
if type == "udp":</div><div> s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)</div><div> else:</div><div> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)</div><div> s.settimeout(5)</div><div>
try:</div><div> if type == "udp":</div><div> s.sendto("--TEST LINE--", (host, port))</div><div> recv, svr = s.recvfrom(255)</div><div> s.shutdown(2)</div><div> print "Success connecting to " + host + " on UDP port: " + str(port)</div>
<div> else:</div><div> s.connect((host, port))</div><div> s.shutdown(2)</div><div> print "Success connecting to " + host + " on TCP port: " + str(port)</div><div> except Exception, e:</div>
<div> try:</div><div> errno, errtxt = e</div><div> except ValueError:</div><div> print "Cannot connect to " + host + " on port: " + str(port)</div><div> else:</div><div> if errno == 107:</div>
<div> print "Success connecting to " + host + " on UDP port: " + str(port)</div><div> else:</div><div> print "Cannot connect to " + host + " on port: " + str(port)</div>
<div> print e</div><div> if test != "C" :</div><div> sys.exit(0)</div><div><br></div><div> s.close</div><div> time.sleep(1)</div><div>--------------------------------------------------------------</div>
<div><br></div><div>Giovanni</div><div><br></div><div>-- </div><div>Giovanni Allegri</div><div><a href="http://about.me/giovanniallegri" target="_blank">http://about.me/giovanniallegri</a></div><div>Twitter: <a href="https://twitter.com/_giohappy_" target="_blank">https://twitter.com/_giohappy_</a></div>
<div>blog: <a href="http://blog.spaziogis.it" target="_blank">http://blog.spaziogis.it</a></div><div>GEO+ geomatica in Italia <a href="http://bit.ly/GEOplus" target="_blank">http://bit.ly/GEOplus</a></div><div class="gmail_extra">
<div dir="ltr"></div>
</div></div>
</blockquote></div>
</div>