src/eric7/EricNetwork/EricJsonServer.py

branch
eric7
changeset 10928
46651e194fbe
parent 10697
8a609e4c71b6
child 11090
f5f5f5803935
equal deleted inserted replaced
10927:ce599998be7d 10928:46651e194fbe
7 Module implementing the JSON based server base class. 7 Module implementing the JSON based server base class.
8 """ 8 """
9 9
10 import contextlib 10 import contextlib
11 import json 11 import json
12 import shutil
12 import struct 13 import struct
13 import time 14 import time
14 import zlib 15 import zlib
15 16
16 from PyQt6.QtCore import ( 17 from PyQt6.QtCore import (
22 QTimer, 23 QTimer,
23 pyqtSlot, 24 pyqtSlot,
24 ) 25 )
25 from PyQt6.QtNetwork import QHostAddress, QTcpServer 26 from PyQt6.QtNetwork import QHostAddress, QTcpServer
26 27
27 from eric7 import Preferences, Utilities 28 from eric7 import EricUtilities
28 from eric7.EricWidgets import EricMessageBox 29 from eric7.EricWidgets import EricMessageBox
29 from eric7.SystemUtilities import FileSystemUtilities
30 30
31 31
32 class EricJsonServer(QTcpServer): 32 class EricJsonServer(QTcpServer):
33 """ 33 """
34 Class implementing a JSON based server base class. 34 Class implementing a JSON based server base class.
35 """ 35 """
36 36
37 def __init__(self, name="", multiplex=False, parent=None): 37 def __init__(self, name="", interface="127.0.0.1", multiplex=False, parent=None):
38 """ 38 """
39 Constructor 39 Constructor
40 40
41 @param name name of the server (used for output only) 41 @param name name of the server (used for output only) (defaults to "")
42 @type str 42 @type str (optional)
43 @param multiplex flag indicating a multiplexing server 43 @param interface network interface to be used (IP address or one of "all",
44 @type bool 44 "allv4", "allv6", "localv4" or "localv6") (defaults to "127.0.0.1")
45 @param parent parent object 45 @type str (optional)
46 @type QObject 46 @param multiplex flag indicating a multiplexing server (defaults to False)
47 @type bool (optional)
48 @param parent reference to the parent object (defaults to None)
49 @type QObject (optional)
47 """ 50 """
48 super().__init__(parent) 51 super().__init__(parent)
49 52
50 self.__name = name 53 self.__name = name
51 self.__multiplex = multiplex 54 self.__multiplex = multiplex
55 else: 58 else:
56 self.__clientProcess = None 59 self.__clientProcess = None
57 self.__connection = None 60 self.__connection = None
58 61
59 # setup the network interface 62 # setup the network interface
60 networkInterface = Preferences.getDebugger("NetworkInterface") 63 if interface in ("allv4", "localv4") or "." in interface:
61 if networkInterface in ("allv4", "localv4") or "." in networkInterface:
62 # IPv4 64 # IPv4
63 self.__hostAddress = "127.0.0.1" 65 self.__hostAddress = "127.0.0.1"
64 elif networkInterface in ("all", "allv6", "localv6"): 66 elif interface in ("all", "allv6", "localv6"):
65 # IPv6 67 # IPv6
66 self.__hostAddress = "::1" 68 self.__hostAddress = "::1"
67 else: 69 else:
68 self.__hostAddress = networkInterface 70 self.__hostAddress = interface
69 self.listen(QHostAddress(self.__hostAddress)) 71 self.listen(QHostAddress(self.__hostAddress))
70 72
71 self.newConnection.connect(self.handleNewConnection) 73 self.newConnection.connect(self.handleNewConnection)
72 74
73 ## Note: Need the address and port if client is started external in debugger. 75 ## Note: Need the address and port if client is started external in debugger.
202 """ could not be decoded. Please report""" 204 """ could not be decoded. Please report"""
203 """ this issue with the received data to the""" 205 """ this issue with the received data to the"""
204 """ eric bugs email address.</p>""" 206 """ eric bugs email address.</p>"""
205 """<p>Error: {0}</p>""" 207 """<p>Error: {0}</p>"""
206 """<p>Data:<br/>{1}</p>""" 208 """<p>Data:<br/>{1}</p>"""
207 ).format(str(err), Utilities.html_encode(jsonString.strip())), 209 ).format(str(err), EricUtilities.html_encode(jsonString.strip())),
208 EricMessageBox.Ok, 210 EricMessageBox.Ok,
209 ) 211 )
210 return 212 return
211 213
212 self.handleCall(clientDict["method"], clientDict["params"]) 214 self.handleCall(clientDict["method"], clientDict["params"])
265 @type dict 267 @type dict
266 @return flag indicating a successful client start and the exit code 268 @return flag indicating a successful client start and the exit code
267 in case of an issue 269 in case of an issue
268 @rtype bool, int 270 @rtype bool, int
269 """ 271 """
270 if interpreter == "" or not FileSystemUtilities.isinpath(interpreter): 272 if interpreter == "" or not bool(shutil.which(interpreter)):
271 return False, -1 273 return False, -1
272 274
273 exitCode = None 275 exitCode = None
274 276
275 proc = QProcess() 277 proc = QProcess()

eric ide

mercurial