Sign up
Login
New paste
Home
Trending
Archive
English
English
Spanish
Sign up
Login
New Paste
Browse
import socket import threading import select import json import uuid import time from Connection import Connection import struct class Server: """ Low level tcp server for manage multiple connections """ def __init__(self, host, port): self.keep_run = True self.host = host self.port = port self.connecitons = [] def set_on_data_handler(self, handler): """callback function to use outside of this class""" self.on_data_handler = handler def set_on_disconnect_handler(self, handler): """callback function to use outside of this class""" self.on_disconnect_handler = handler def set_on_connection_handler(self, handler): """callback function to use outside of this class""" self.on_connection_handler = handler def data_channel(self, connection): """get new messages, and disconnect clients on disconnect""" conn_id, addr, conn = connection.asdict().values() while self.keep_run: msglen = self.recvall(conn, 4) msglen = struct.unpack('>I', msglen)[0] data = self.recvall(conn, msglen) if data: self.data_handler(data, connection) else: print('no data') conn.close() for i in range(len(self.connecitons)): if self.connecitons[i].id == connection.id: del self.connecitons[i] break self.on_disconnect_handler(connection) break conn.close() def recvall(self, conn, n): """Receive packet by size (buffer)""" data = b'' while len(data) < n: packet = conn.recv(n - len(data)) if not packet: return None data += packet return data def data_handler(self, data, connection): """handle and parse data from clients""" data = data.decode() data = json.loads(data) self.on_data_handler(data, connection) def connection_handler(self, connection): """Setup data channel thread""" self.connecitons.append(connection) self.on_connection_handler(connection) t = threading.Thread(target=self.data_channel, args=(connection,)) t.daemon = True t.start() def get_connections(self): """Get new connection thread""" while self.keep_run: s = socket.socket() s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.bind((self.host, self.port)) s.listen(0) print('waiting....') conn, addr = s.accept() print(f'connected from {addr}') connection = Connection(addr, conn, uuid.uuid4().hex) self.connection_handler(connection) conn.close() def start(self): """start get connection thread""" print('server started...') t = threading.Thread(target=self.get_connections) t.daemon = True t.start() def close(self): """Stop the server (and threads)""" print('shutting down...') self.keep_run = False
Paste Settings
Paste Title :
[Optional]
Paste Folder :
[Optional]
Select
Syntax Highlighting :
[Optional]
Select
Markup
CSS
JavaScript
Bash
C
C#
C++
Java
JSON
Lua
Plaintext
C-like
ABAP
ActionScript
Ada
Apache Configuration
APL
AppleScript
Arduino
ARFF
AsciiDoc
6502 Assembly
ASP.NET (C#)
AutoHotKey
AutoIt
Basic
Batch
Bison
Brainfuck
Bro
CoffeeScript
Clojure
Crystal
Content-Security-Policy
CSS Extras
D
Dart
Diff
Django/Jinja2
Docker
Eiffel
Elixir
Elm
ERB
Erlang
F#
Flow
Fortran
GEDCOM
Gherkin
Git
GLSL
GameMaker Language
Go
GraphQL
Groovy
Haml
Handlebars
Haskell
Haxe
HTTP
HTTP Public-Key-Pins
HTTP Strict-Transport-Security
IchigoJam
Icon
Inform 7
INI
IO
J
Jolie
Julia
Keyman
Kotlin
LaTeX
Less
Liquid
Lisp
LiveScript
LOLCODE
Makefile
Markdown
Markup templating
MATLAB
MEL
Mizar
Monkey
N4JS
NASM
nginx
Nim
Nix
NSIS
Objective-C
OCaml
OpenCL
Oz
PARI/GP
Parser
Pascal
Perl
PHP
PHP Extras
PL/SQL
PowerShell
Processing
Prolog
.properties
Protocol Buffers
Pug
Puppet
Pure
Python
Q (kdb+ database)
Qore
R
React JSX
React TSX
Ren'py
Reason
reST (reStructuredText)
Rip
Roboconf
Ruby
Rust
SAS
Sass (Sass)
Sass (Scss)
Scala
Scheme
Smalltalk
Smarty
SQL
Soy (Closure Template)
Stylus
Swift
TAP
Tcl
Textile
Template Toolkit 2
Twig
TypeScript
VB.Net
Velocity
Verilog
VHDL
vim
Visual Basic
WebAssembly
Wiki markup
Xeora
Xojo (REALbasic)
XQuery
YAML
HTML
Paste Expiration :
[Optional]
Never
Self Destroy
10 Minutes
1 Hour
1 Day
1 Week
2 Weeks
1 Month
6 Months
1 Year
Paste Status :
[Optional]
Public
Unlisted
Private (members only)
Password :
[Optional]
Description:
[Optional]
Tags:
[Optional]
Encrypt Paste
(
?
)
Create New Paste
You are currently not logged in, this means you can not edit or delete anything you paste.
Sign Up
or
Login
Site Languages
×
English
Spanish