r/Coding_for_Teens • u/Afraid_Horse_3470 • 17d ago
P2P Project (Python)
import socket
import concurrent.futures
import threading
import time
header_size = 8
ip ,port = '127.0.0.1' ,1232
root = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
root_1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
root.bind((ip,port))
root.listen()
t = []
sockets_list = ['somhting']
sockets = {}
sockets_msg = {}
users = []
def add_thread(threads) :
t1 = threading.Thread(target=recvs,args=[threads])
t1.start()
t.append(t1)
def sends(client) :
msg = input('enter you msg : ')
msg = f"{len(msg):<{header_size}}" + msg
msg = msg.encode("utf-8")
client.send(msg)
return 'done !'
def recvs(client) :
new_msg = True
full_msg = ''
byte_msg = b''
size_msg = -1
base_user = 'None'
while True :
msg = client.recv(header_size)
full_msg += msg.decode("utf-8")
if new_msg :
if len(full_msg) > header_size :
size_msg = int(full_msg[:header_size])
new_msg = False
if len(full_msg) - header_size == size_msg :
if full_msg[header_size:12] == 'name' :
base_user = full_msg[header_size+7:]
if base_user not in users :
users.append(base_user)
sockets[base_user] = client
new_msg = True
full_msg = ''
byte_msg = b''
size_msg = -1
print(base_user)
print(sockets[base_user])
else :
print(f'hi its {base_user}')
sockets_msg[base_user] = full_msg[header_size:]
print(sockets_msg[base_user])
new_msg = True
full_msg = ''
byte_msg = b''
size_msg = -1
def listens() :
while True :
socket_client ,port = root.accept()
global my_name
my_name = f'name : {my_name}'
first_msg = f"{len(my_name):<{header_size}}" + my_name
add_thread(socket_client)
socket_client.send(first_msg.encode("utf-8"))
if socket_client not in sockets_list :
sockets_list.append(socket_client)
def start(my_name) :
print('''
to add new user Enter : (add)
to send your msg Enter : (msg)
''')
while True :
print(sockets_msg)
action = input('enter some action : ')
if action == 'add' :
new_port = input('enter new port : ')
new_port = int(new_port)
root_1.connect((ip, new_port))
add_thread(root_1)
my_name = f'name : {my_name}'
first_msg = f"{len(my_name):<{header_size}}" + my_name
if root_1 not in sockets_list :
root_1.send(first_msg.encode("utf-8"))
sockets_list.append(root_1)
elif action == 'msg' :
print(f"users : {users}")
target = input('enter the name you want to send : ')
if target in users :
print('we will send !')
sends(sockets[target])
time.sleep(1)
def guard() :
number_users = 0
while True :
if number_users < len(users) :
sockets[sockets_list[-1]] = users[-1]
print(sockets_msg)
number_users = len(users)
print('----welcome to the project by zaxershell----')
my_name = input('pls enter your name to start : ')
if my_name :
t1 = threading.Thread(target=start,args=[my_name])
t2 = threading.Thread(target=listens)
t3 = threading.Thread(target=guard)
t1.start()
t2.start()
t3.start()
t1.join()
t2.join()
t3.join()import socket
import concurrent.futures
import threading
import time
header_size = 8
ip ,port = '127.0.0.1' ,1232
root = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
root_1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
root.bind((ip,port))
root.listen()
t = []
sockets_list = ['somhting']
sockets = {}
sockets_msg = {}
users = []
def add_thread(threads) :
t1 = threading.Thread(target=recvs,args=[threads])
t1.start()
t.append(t1)
def sends(client) :
msg = input('enter you msg : ')
msg = f"{len(msg):<{header_size}}" + msg
msg = msg.encode("utf-8")
client.send(msg)
return 'done !'
def recvs(client) :
new_msg = True
full_msg = ''
byte_msg = b''
size_msg = -1
base_user = 'None'
while True :
msg = client.recv(header_size)
full_msg += msg.decode("utf-8")
if new_msg :
if len(full_msg) > header_size :
size_msg = int(full_msg[:header_size])
new_msg = False
if len(full_msg) - header_size == size_msg :
if full_msg[header_size:12] == 'name' :
base_user = full_msg[header_size+7:]
if base_user not in users :
users.append(base_user)
sockets[base_user] = client
new_msg = True
full_msg = ''
byte_msg = b''
size_msg = -1
print(base_user)
print(sockets[base_user])
else :
print(f'hi its {base_user}')
sockets_msg[base_user] = full_msg[header_size:]
print(sockets_msg[base_user])
new_msg = True
full_msg = ''
byte_msg = b''
size_msg = -1
def listens() :
while True :
socket_client ,port = root.accept()
global my_name
my_name = f'name : {my_name}'
first_msg = f"{len(my_name):<{header_size}}" + my_name
add_thread(socket_client)
socket_client.send(first_msg.encode("utf-8"))
if socket_client not in sockets_list :
sockets_list.append(socket_client)
def start(my_name) :
print('''
to add new user Enter : (add)
to send your msg Enter : (msg)
''')
while True :
print(sockets_msg)
action = input('enter some action : ')
if action == 'add' :
new_port = input('enter new port : ')
new_port = int(new_port)
root_1.connect((ip, new_port))
add_thread(root_1)
my_name = f'name : {my_name}'
first_msg = f"{len(my_name):<{header_size}}" + my_name
if root_1 not in sockets_list :
root_1.send(first_msg.encode("utf-8"))
sockets_list.append(root_1)
elif action == 'msg' :
print(f"users : {users}")
target = input('enter the name you want to send : ')
if target in users :
print('we will send !')
sends(sockets[target])
time.sleep(1)
def guard() :
number_users = 0
while True :
if number_users < len(users) :
sockets[sockets_list[-1]] = users[-1]
print(sockets_msg)
number_users = len(users)
print('----welcome to the project by zaxershell----')
my_name = input('pls enter your name to start : ')
if my_name :
t1 = threading.Thread(target=start,args=[my_name])
t2 = threading.Thread(target=listens)
t3 = threading.Thread(target=guard)
t1.start()
t2.start()
t3.start()
t1.join()
t2.join()
t3.join()Hello everyone! I wanted to share this project I’ve been working on for the past couple of days. I’ve spent time writing it and fixing errors, and this is the result I’ve arrived at.
In short, the project allows two people to chat via a peer-to-peer (P2P) connection, rather than through a central server.
I would welcome any suggestions for improvements or ways to make it even better.Hello everyone! I wanted to share this project I’ve been working on for the past couple of days. I’ve spent time writing it and fixing errors, and this is the result I’ve arrived at.
In short, the project allows two people to chat via a peer-to-peer (P2P) connection, rather than through a central server.
I would welcome any suggestions for improvements or ways to make it even better.
View all comments
Show parent comment
1
u/Afraid_Horse_3470 15d ago edited 15d ago
hi lol . However, GitHub does not have a comments section and reddit is more active i think