add and test web servers
This commit is contained in:
parent
2ab03678bf
commit
b24d642f74
9
livingPlotPanel/Register.py
Normal file
9
livingPlotPanel/Register.py
Normal file
@ -0,0 +1,9 @@
|
||||
|
||||
|
||||
|
||||
class Register:
|
||||
|
||||
def __init__(self) -> None:
|
||||
pass
|
||||
|
||||
|
83
livingPlotPanel/WebServer.py
Normal file
83
livingPlotPanel/WebServer.py
Normal file
@ -0,0 +1,83 @@
|
||||
import socket
|
||||
from PyQt5 import QtCore
|
||||
|
||||
|
||||
def print_new_message(msg):
|
||||
print(msg)
|
||||
|
||||
|
||||
class webServer(QtCore.QObject):
|
||||
|
||||
newMessageSignal = QtCore.pyqtSignal(str)
|
||||
|
||||
def __init__(self, host='localhost', port=12321) -> None:
|
||||
|
||||
self.host = host
|
||||
self.port = port
|
||||
self.socket = None
|
||||
self.server = None
|
||||
self.serverAddr = None
|
||||
|
||||
QtCore.QObject.__init__(self)
|
||||
|
||||
self.newMessageSignal.connect(print_new_message)
|
||||
|
||||
def __enter__(self):
|
||||
|
||||
# create a socket connection
|
||||
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
# bind the socket with host and port
|
||||
self.socket.bind((self.host, self.port))
|
||||
# allow maximum 1 connection to the socket
|
||||
self.socket.listen(1)
|
||||
|
||||
return self
|
||||
|
||||
def __exit__(self, *args):
|
||||
self.socket.close()
|
||||
|
||||
def close(self):
|
||||
self.socket.close()
|
||||
|
||||
def start_listening(self):
|
||||
|
||||
while True:
|
||||
# wait till a client accept connection
|
||||
self.server, self.serverAddr = self.socket.accept()
|
||||
|
||||
msg = self.server.recv(1024)
|
||||
msg = msg.decode()
|
||||
self.newMessageSignal.emit(msg)
|
||||
|
||||
|
||||
class webClient(object):
|
||||
|
||||
def __init__(self, host='localhost', port=12321) -> None:
|
||||
|
||||
self.host = host
|
||||
self.port = port
|
||||
self.socket = None
|
||||
self.client = None
|
||||
|
||||
def __enter__(self):
|
||||
|
||||
# create a socket connection
|
||||
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
|
||||
# connect it to server and port number on local computer.
|
||||
self.socket.connect((self.host, self.port))
|
||||
self.client = self.socket
|
||||
|
||||
return self
|
||||
|
||||
def __exit__(self, *args):
|
||||
self.socket.close()
|
||||
|
||||
def close(self):
|
||||
self.socket.close()
|
||||
|
||||
def connect(self):
|
||||
self.socket.connect((self.host, self.port))
|
||||
|
||||
def send(self, msg):
|
||||
self.client.send(msg.encode())
|
12
livingPlotPanel/testWebClient.py
Normal file
12
livingPlotPanel/testWebClient.py
Normal file
@ -0,0 +1,12 @@
|
||||
from WebServer import webServer, webClient
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
host = 'localhost'
|
||||
port = 12321
|
||||
|
||||
with webClient(host, port) as client:
|
||||
client.send('hello')
|
||||
client.send('hello')
|
||||
client.send('hello')
|
||||
client.send('hello')
|
9
livingPlotPanel/testWebServer.py
Normal file
9
livingPlotPanel/testWebServer.py
Normal file
@ -0,0 +1,9 @@
|
||||
from WebServer import webServer, webClient
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
host = 'localhost'
|
||||
port = 12321
|
||||
|
||||
with webServer(host, port) as server:
|
||||
server.start_listening()
|
@ -1,5 +1,14 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from PyQt5.QtCore import (Qt, pyqtSignal)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
@ -1631,46 +1640,6 @@
|
||||
"dataSet"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 16,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"(11, 3)"
|
||||
]
|
||||
},
|
||||
"execution_count": 16,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"dataSet.shotNum.shape"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 17,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"['atoms', 'background', 'dark', 'shotNum', 'OD']"
|
||||
]
|
||||
},
|
||||
"execution_count": 17,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"list(dataSet.data_vars)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
|
Loading…
Reference in New Issue
Block a user