-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatserver.py
More file actions
60 lines (49 loc) · 1.58 KB
/
chatserver.py
File metadata and controls
60 lines (49 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
'''
Plan-
parse command 1st
server connnection
client connection
#annoying ahh need to add multithreading so multiple chatroom clienjts and work
'''
# Importing Libraries
import argparse
import socket
import threading
import rsa
# Server Shit
public_key,private_key=rsa.newkeys(1024)
public_partner=None
HOST_IP = "127.0.0.1" # Non Routable Meta Address
HOST_PORT = 6666 # Designated Port
def client_handler(client_socket, client_address):
while True:
client_socket.send(public_key.save_pkcs1("PEM"))
data = rsa.decrypt(client_socket.recv(1024), private_key).decode()
if not data:
break
print(f"Data Recieved: {client_address}: {data}")
client_socket.close()
def start_server():
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind((HOST_IP, HOST_PORT))
server_socket.listen()
print(f"Server is listening on {HOST_IP}:{HOST_PORT}")
while True:
client_socket, client_address = server_socket.accept()
print(f"Connected to {client_address}")
threading.Thread(target=client_handler, args=(client_socket, client_address)).start()
start_server()
'''
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # AF_INET (IPv4) & SOCK_STREAM (TCP)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST_IP, HOST_PORT))
s.listen()
conn, addr = s.accept()
with conn:
print(f"Connection Established - {addr}")
while True:
data = conn.recv(1024)
if not data:
break
conn.sendall(data)
'''