Last active
December 12, 2021 10:58
-
-
Save themixray/5acefef8ffa00e5ad8427ac60c9ceded to your computer and use it in GitHub Desktop.
communication python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import communicate as c | |
s = c.client('localhost',int(input('port> '))) | |
while 1: | |
s.send(input('send> ')) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pyngrok import ngrok, conf | |
import threading | |
import pickle | |
import socket | |
class server: | |
def __init__(self, port=0, ngrokToken='', ngrokPath=''): | |
self._started = False | |
self._thread = None | |
self._isThread = False | |
self.port = port | |
self.ngrokToken = ngrokToken | |
self.ngrokPath = ngrokPath | |
def start(self, callback=lambda x,y:None, thread=True): | |
self._isThread = thread | |
self._callback = callback | |
if self._started: | |
self.stop() | |
self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
self._sock.bind(('', self.port)) | |
self.port = self._sock.getsockname()[1] | |
self._sock.listen(5) | |
if self.ngrokToken != '': | |
if self.ngrokPath != '': | |
conf.set_default(conf.PyngrokConfig(ngrok_path=self.ngrokPath)) | |
ngrok.set_auth_token(self.ngrokToken) | |
self._ip = ngrok.connect(self.port, 'tcp') | |
self.ip = self._ip.public_url.split('://')[1] | |
self.port = int(self.ip.split(':')[1]) | |
self.ip = self.ip.split(':')[0] | |
else: | |
self.ip = socket.gethostbyname(socket.gethostname()) | |
self._started = True | |
def main(self): | |
while self._started: | |
conn, addr = self._sock.accept() | |
data = conn.recv(1024*1024*2) | |
callback = self._callback(pickle.loads(data),addr) | |
if callback != None: | |
conn.send(pickle.dumps(callback)) | |
conn.close() | |
main0 = main | |
main = lambda:main0(self) | |
if self._isThread: | |
self._thread = threading.Thread(target=main) | |
self._thread.start() | |
else: | |
main() | |
self.stop() | |
return (self.ip, self.port) | |
def stop(self): | |
if self.ngrokToken != '': | |
ngrok.disconnect(self._ip.public_url) | |
ngrok.kill() | |
self._started = False | |
self._sock.close() | |
if self._isThread: | |
self._thread.join() | |
self._thread = None | |
class client: | |
def __init__(self, ip, port): | |
self.ip = ip | |
self.port = port | |
def send(self, data): | |
sock = socket.socket(socket.AF_INET, | |
socket.SOCK_STREAM) | |
sock.connect((self.ip, self.port)) | |
sock.send(pickle.dumps(data)) | |
chunks = [] | |
bytes_recd = 0 | |
while bytes_recd < MSGLEN: | |
chunk = sock.recv(min(MSGLEN - bytes_recd, 2048)) | |
if chunk == b'': | |
raise RuntimeError("socket connection broken") | |
chunks.append(chunk) | |
bytes_recd = bytes_recd + len(chunk) | |
recv = b''.join(chunks) | |
if recv != None: | |
recv = pickle.loads() | |
sock.close() | |
return recv |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import communicate as c | |
s = c.server() | |
def callback(data,addr): | |
print(addr, '>', data) | |
s.start(callback) | |
print('port:',s.port) | |
input('close> ') | |
s.stop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment