1 year ago

#358141

test-img

printrman

Why does my Python P2P client works over LAN but not the Internet and how can I fix this

Why does my Python P2P client works over LAN but not the Internet and how can I fix this!

I would like to make a p2p messenger and I just don't know why the p2p functionality with UDP Hole Punching is not working. Please help!

Server: This server holds peers ips and ports for the client. The ping function pings the peers to check if they are still alive. ''' import threading import socket import time

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.bind(("IP_ADDRESS", 8081))

addrs = [] print('Server started...')

def ping(addr): try: sock.sendto(b'ping', addr) sock.settimeout(1) data, addr = sock.recvfrom(1024) sock.settimeout(None) return True except Exception as ex: sock.settimeout(None) return False

while True: data, addr = sock.recvfrom(1024) print(addr) sock.sendto((addr[0]+' '+str(addr[1])).encode(), addr) for a in addrs: p = ping(a) print(p, a) if p: sock.sendto((a[0]+' '+str(a[1])).encode(), addr) addrs.append(addr) sock.sendto(b'DONE', addr) '''

Client:

import threading
import socket
import time

        self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.addrs = []
        self.chat_logs = ''
        self.peers = ''
        self.self_data = ('0.0.0.0', 0)

    def recv_thread(self):
        while True:
            try:
                data, addr = self.sock.recvfrom(1024)
                if data == b'ping':
                    self.sock.sendto(b'pong', addr)
                else:
                    data = data.decode()
                    new_user = True
                    for a in self.addrs:
                        if a == addr:
                            new_user = False
                    if new_user == False:
                        self.chat_logs += addr[0]+':'+str(addr[1])+' - '+data+'\n'
                    else:
                        self.chat_logs += addr[0]+':'+str(addr[1])+' joined the p2p network... \n'
                        self.addrs.append(addr)
            except Exception as ex:
                print(ex)

    def send_msg(self):
        message = self.messageBox.text()
        if len(message) > 0:
            self.chat_logs += 'You - '+message+'\n'
            self.chatBox.setPlainText(self.chat_logs)
            for a in self.addrs:
                try:
                    self.sock.sendto(message.encode(), a)
                except:
                    self.addrs.remove(a)

    def get_peers(self, host):
        host = host.split(':')
        host = (host[0], int(host[1]))

        self.sock.sendto(b'PEERS', host)
        self.self_data, addr = self.sock.recvfrom(512)
        self.self_data = self.self_data.decode().split()
        self.self_data = (self.self_data[0], int(self.self_data[1]))
        while True:
            data, addr = self.sock.recvfrom(512)
            if data == b'DONE':
                break
            else:
                data = data.decode().split()
                self.addrs.append((data[0], int(data[1])))
        print(self.addrs)
        for a in self.addrs:
            try:
                self.sock.sendto(b'join', a)
            except:
                self.addrs.remove(a)
        t = threading.Thread(target=self.recv_thread)
        t.start()

    def connect_to_pears_network(self):
        dlg = CustomDialog()
        if dlg.exec():
            text = dlg.message.text()
            if len(text) > 6:
                self.get_peers(text)
        else:
            pass

I hope I did this post right this is my first time making a post.

python

python-3.x

networking

p2p

lan

0 Answers

Your Answer

Accepted video resources