[Qgis-user] Add a wms layer from startu macro

G. Allegri giohappy at gmail.com
Thu Apr 24 04:55:34 PDT 2014


You can write a startup.py file and put it into the <user>/.qgis2/python
folder.
It will be loaded and executed, so you could check for internet
availability with something as simple as:

------------------------------------------------------
import urllib2
url = <your_wms_server_getcapabilities_service>
try:
   urllib2.urlopen(url)
except urllib2.URLError, e:
   print "Could not connect"
-----------------------------------------------------

Last time I needed it I used the following (with various adaptations):

-----------------------------------------------------
import sys
import socket
import time

host = <my_url>
port = <my_port_number>
# type => ["tcp" or "udp"]
type = sys.argv[3]
test = ""
if len(sys.argv) > 4 :
 test = sys.argv[4]

while 1 :
  if type == "udp":
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  else:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  s.settimeout(5)
  try:
    if type == "udp":
      s.sendto("--TEST LINE--", (host, port))
      recv, svr = s.recvfrom(255)
      s.shutdown(2)
      print "Success connecting to " + host + " on UDP port: " + str(port)
    else:
      s.connect((host, port))
      s.shutdown(2)
      print "Success connecting to " + host + " on TCP port: " + str(port)
  except Exception, e:
    try:
      errno, errtxt = e
    except ValueError:
      print "Cannot connect to " + host + " on port: " + str(port)
    else:
      if errno == 107:
        print "Success connecting to " + host + " on UDP port: " + str(port)
      else:
        print "Cannot connect to " + host + " on port: " + str(port)
        print e
  if test != "C" :
    sys.exit(0)

  s.close
  time.sleep(1)
--------------------------------------------------------------

Giovanni

-- 
Giovanni Allegri
http://about.me/giovanniallegri
Twitter: https://twitter.com/_giohappy_
blog: http://blog.spaziogis.it
GEO+ geomatica in Italia http://bit.ly/GEOplus
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20140424/c803f649/attachment.html>


More information about the Qgis-user mailing list