Yam Code
Sign up
Login
New paste
Home
Trending
Archive
English
English
Tiếng Việt
भारत
Sign up
Login
New Paste
Browse
import socket, threading from PyQt5 import QtWidgets as qtw import os import sys import json import time from UI_Server import Ui_Server BUFSIZE = 2048 clients = [] SERVER = "" FORMAT = 'utf-8' class MainServer(qtw.QMainWindow): def __init__(self) : self.isOpen = False qtw.QMainWindow.__init__(self) self.ui = Ui_Server() self.ui.setupUi(self) self.show() self.total_clients = 0 #Event self.ui.txtPort.textChanged.connect(self.inputPort) self.ui.txtNumber.textChanged.connect(self.inputNumber) self.ui.buttonOpen.clicked.connect(self.openServer) self.ui.buttonClose.clicked.connect(self.closeServer) #def disableButton(self): #self.ui.pushButton.setEnabled(False) #----------------------------------------------------------------------- def closeServer(self): #try: self.ui.textBrowser.append("Close all connection") self.server.close() #event.accept() #except OSError: #event.accept() def inputPort(self): str(self.ui.txtPort.text()) def inputNumber(self): str(self.ui.txtNumber.text()) def openServer(self): self.start() def closeEvent(self,event): self.destroy() os._exit(0) #----------------------------------------------------------------------- def newConnection(self, server): while True: print(len(clients)) if int(self.ui.txtNumber.text()) == len(clients): self.ui.textBrowser.append("Reached maximum connection") break else: connection, client_addr = self.server.accept() clients.append(self.Client(self, connection, client_addr, True)) # add new client and waiting for it's repsonse clients[len(clients) - 1].start() self.ui.textBrowser.append(str(client_addr) + " has connected to server") #self.handle_client(connection, client_addr) #try: # except socket.error as e: # self.ui.textBrowser.append(str(client_addr) + " has disconnected") # break def start(self): self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # reuse socket after being closed immediately self.server.bind((SERVER, int(self.ui.txtPort.text()))) self.ui.textBrowser.append("Waiting for connection") self.server.listen(5) serverThread = threading.Thread(target = self.newConnection, args = (self.server, )) serverThread.start() #-------------------------------------------------------------------------------- class Client( threading.Thread): def __init__(self, server, socket, address, signal): threading.Thread.__init__(self) self.socket = socket self.address = address self.signal = signal self.server = server def __str__(self): return str(self.address) def run(self): while self.signal: try: cmd = self.socket.recv(1024) cmd = cmd.decode() except: self.server.ui.textBrowser.append("Client " + str(self.address) + " has disconnected") self.signal = False clients.remove(self) break #---------------------------------------------------------------------------------------------------------- if cmd == 'signin': print("signin") caseS = signIn(self.socket) if caseS == 0: #signUp UNSUCESSFUL msg = "SignIn false" self.socket.send(bytes(msg, "utf-8")) elif caseS == 2: msg = "Admin SignIn successful" self.socket.send(bytes(msg, "utf-8")) self.server.ui.textBrowser.append("admin has Sing In") else: msg = "SignIn successful" self.socket.send(bytes(msg, "utf-8")) self.server.ui.textBrowser.append(str(self.address) + " has Sing In") elif cmd == "signUp": print("signup") if signUp(self.socket) == 0: msg = "SignUp false" self.socket.send(bytes(msg, "utf-8")) else: msg = "SignUp Sucessful" self.socket.send(bytes(msg, "utf-8")) self.server.ui.textBrowser.append(str(self.address) + " has Sing Up") elif cmd == "listall": sendListAll(self.socket) #--------------------------------------------------------------------------- def sendListAll(connection): THIS_FOLDER = os.path.dirname(os.path.abspath(__file__)) #deriving the current path of file my_file = os.path.join(THIS_FOLDER, 'matches.json') f = open(my_file, 'r') data = json.load(f) f.close() data = json.dumps(data) size = sys.getsizeof(data) size = str(size) connection.send(bytes(size, "utf-8")) #send size time.sleep(0.02) connection.sendall(bytes(data, "utf-8")) ##################################################################### def checkSignIn(username, password): THIS_FOLDER = os.path.dirname(os.path.abspath(__file__)) #deriving the current path of file my_file = os.path.join(THIS_FOLDER, 'data.txt') f = open(my_file,'r') #read number of clients num_clients = f.readline() num_clients = int(num_clients) for i in range(1, num_clients + 1): signin_user = f.readline() signin_user = signin_user.strip() signin_pass = f.readline() signin_pass = signin_pass.strip() signin_id = f.readline() signin_id = int(signin_id.strip()) if (signin_user == username and signin_pass == password): if signin_id == 1: #1-ADMIN #0-Client return 2 #Return TRUE else: return 1 return 0 #Return FALSE def checkUsername(username): THIS_FOLDER = os.path.dirname(os.path.abspath(__file__)) #deriving the current path of file my_file = os.path.join(THIS_FOLDER, 'data.txt') f = open(my_file,'r') num_clients = f.readline() num_clients = int(num_clients) for i in range(1, num_clients + 1): signin_user = f.readline() signin_user = signin_user.strip() signin_pass = f.readline() signin_pass = signin_pass.strip() if signin_user == username: return 0 return 1 def signIn(connection): print("join sign in") data = connection.recv(BUFSIZE) username = data.decode("utf-8") data = connection.recv(BUFSIZE) password = data.decode("utf-8") case_signin = checkSignIn(username, password) return case_signin def signUp(connection): data = connection.recv(BUFSIZE) username = data.decode("utf-8") data = connection.recv(BUFSIZE) password = data.decode("utf-8") #check UserName case_signUp = checkUsername(username) if case_signUp == 0: #Username is already used return 0 THIS_FOLDER = os.path.dirname(os.path.abspath(__file__)) #deriving the current path of file my_file = os.path.join(THIS_FOLDER, 'data.txt') f = open(my_file,'r') lst=[] num_clients = f.readline() num_clients = int(num_clients) lst.append(str(num_clients)) for i in range(1, num_clients+1): signUp_user = f.readline() signUp_user = signUp_user.strip() lst.append(signUp_user) signUp_pass = f.readline() signUp_pass = signUp_pass.strip() lst.append(signUp_pass) signUp_role = f.readline() signUp_role = signUp_role.strip() lst.append(signUp_role) f.close() lst.append(username) lst.append(password) lst.append(str(0)) fout = open(my_file,'w') lst[0] = str(int(lst[0]) + 1) fout.write(lst[0]) fout.write("\n") for i in range(1, len(lst)): fout.write(lst[i]) fout.write("\n") fout.close() return 1 #Sign Up Succesful if __name__ == "__main__": app = qtw.QApplication(sys.argv) ui = MainServer() sys.exit(app.exec_())
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
Tiếng Việt
भारत