You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
96 lines
2.9 KiB
Python
96 lines
2.9 KiB
Python
# Python 3 server example
|
|
from http.server import BaseHTTPRequestHandler, HTTPServer
|
|
import time
|
|
import _thread as thread
|
|
import requests
|
|
import os
|
|
import toml
|
|
|
|
#file_path = os.path.dirname(os.path.realpath(__file__))
|
|
file_path = "D:/Daten/nc/Ingenieurbüro/98-CAD-Daten/Tools-QGIS/ibpTools/"
|
|
|
|
with open(file_path + 'config.toml', 'r') as f:
|
|
config = toml.load(f)
|
|
|
|
hostName = config['iface']['host']
|
|
serverPort = config['iface']['port']
|
|
|
|
class IBP_ToolsWebserver(HTTPServer):
|
|
|
|
def __init__(self, address, handler, iface):
|
|
self.keepalive = True
|
|
self.iface = iface
|
|
#super().__init__(address, handler)
|
|
|
|
def handler(*args):
|
|
IBP_ToolsWebserverRequestHandler(iface, *args)
|
|
|
|
super().__init__((hostName, serverPort), handler)
|
|
print("Server started http://%s:%s" % address)
|
|
|
|
def run_server(self):
|
|
self.keepalive = True
|
|
|
|
while self.keepalive:
|
|
self.handle_request()
|
|
self.server_close()
|
|
print("gestoppt")
|
|
|
|
def stop_server(self):
|
|
self.keepalive = False
|
|
print("Keepalive = False")
|
|
requests.get('http://{}:{}'.format(hostName, serverPort))
|
|
|
|
|
|
|
|
class IBP_ToolsWebserverRequestHandler(BaseHTTPRequestHandler):
|
|
def __init__(self, iface, *args):
|
|
|
|
self.iface = iface
|
|
BaseHTTPRequestHandler.__init__(self, *args)
|
|
#super().__init__(self, *args)
|
|
#super().__init__(request, client_address, server)
|
|
|
|
def do_GET(self):
|
|
|
|
cmd = self.path.split("/")
|
|
|
|
if cmd[1] == "canvas":
|
|
if cmd[2] == "center":
|
|
print("move to ....")
|
|
x = float(cmd[3].split(",")[0])
|
|
y = float(cmd[3].split(",")[1])
|
|
self.iface.mapCanvas().setCenter(QgsPointXY(x ,y))
|
|
|
|
self.send_response(200)
|
|
self.send_header("Content-type", "text/html")
|
|
self.end_headers()
|
|
self.wfile.write(bytes("<html><head><title>https://pythonbasics.org</title></head>", "utf-8"))
|
|
self.wfile.write(bytes("<p>Request: %s</p>" % self.path, "utf-8"))
|
|
self.wfile.write(bytes("<body>", "utf-8"))
|
|
self.wfile.write(bytes("<p>This is an example web server.</p>", "utf-8"))
|
|
self.wfile.write(bytes("</body></html>", "utf-8"))
|
|
|
|
|
|
webServer = IBP_ToolsWebserver((hostName, serverPort), IBP_ToolsWebserverRequestHandler, iface)
|
|
# start the server in a background thread
|
|
thread.start_new_thread(webServer.run_server, ())
|
|
|
|
print('The server is running but my script is still executing!')
|
|
|
|
#webServer.stop_server()
|
|
|
|
|
|
|
|
"""
|
|
https://qgis.org/pyqgis/3.4/gui/QgsMapCanvas.html
|
|
zoomToFeatureIds
|
|
|
|
iface.mapCanvas().setCenter(QgsPointXY(797040.360,5306381.879))
|
|
/canvas/center/797040.360,5306381.879
|
|
|
|
http://localhost:8080/test
|
|
http://localhost:8080/canvas/center/797040.360,5306381.879
|
|
|
|
|
|
""" |