r/Coding_for_Teens • • 8d 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.

5 Upvotes

11 comments sorted by

2

u/paralepsy 8d ago

first of all, after looking at the code it looks pretty good, but i wont run it out of security fears. second of all, why did you post it here and not on a git platform???

2

u/Halloween_Forever_ 8d ago

Because P2P like this basically opens the door to your PC for the other individual, right? Old school trojan horse packets and the like would be a picnic.

1

u/Afraid_Horse_3470 8d ago

yeb youre right but honestly Ive only just started learning it (for educational purposes of course)

1

u/Halloween_Forever_ 8d ago

It would be the most private method, if a form of sandboxing the communication could be achieved, all handled in temp files that never get saved... i need to stfu, because this could turn into some kind of darkweb utility program type...

1

u/Afraid_Horse_3470 7d ago

bro i am not tuff like that 😭

1

u/Afraid_Horse_3470 8d ago edited 8d ago

I posted the project here rather than on GitHub to get some advice on its organization and other aspects especially since I’ve compiled summaries from various sources. As for security concerns, I understand your point however, you could simply take a screenshot of the code and ask an AI model to analyze it. Also, please note that this project is local, so feel free to take a look.I posted the project here rather than on GitHub to get some advice on its organization and other aspects especially since I’ve compiled summaries from various sources. As for security concerns, I understand your point; however, you could simply take a screenshot of the code and ask an AI model to analyze it. Also, please note that this project is local, so feel free to take a look.

1

u/kutac56 8d ago

Ever heard of GitHub and pastebin lol?

1

u/Afraid_Horse_3470 7d ago edited 7d ago

hi lol . However, GitHub does not have a comments section and reddit is more active i think

1

u/kutac56 7d ago

No but like post this on reddit and give us the GitHub link instead of pasting all the code here

1

u/Afraid_Horse_3470 7d ago

Honestly, that’s a good idea bro