Python Peer-to-peer way to achieve simple UDP hole

Source: Internet
Author: User
Tags socket split thread

The original is my graduation design. Now take it out and share it. First of all, the author is lust, if the teacher found on the Internet This section of code can not say that my graduation design is online copy drop ha.

Using Python to do a peer-to-peer simple implementation, you can through the server to register the public IP and port, directly with other clients to communicate. This can only be achieved successfully under full symmetric NAT, with the Windows 2003 NAT service and VMware-band NAT tested successfully. Using iptables NAT is not a good line.

Beginners Python, the code is not very perfect, such as Logout did not write, only for reference.

Server-side code:

#!/usr/bin/python #coding: utf-8 import socket, sys, socketserver, threading, Thread, time Server_port = 1234 Sock = Socket.socket (socket.af_inet, socket. SOCK_DGRAM) sock.setsockopt (socket. Sol_socket, SOCKET.     
        SO_REUSEADDR, 1) sock.bind ((', server_port)) user_list = [] def server_handle (): While True: Cli_date, Cli_pub_add = Sock.recvfrom (8192) now_user = [] Headder = [] Cl    
            I_str = {} Headder = Cli_date.split (' \ t ') for one_line in headder:str = {}    
                
        str = One_line args = Str.split (': ') cli_str[args[0]] = args[1]    
            If cli_str[' type '] = = ' Login ': del cli_str[' type '] Now_user = cli_str    
            now_user[' cli_pub_ip '] = cli_pub_add[0] now_user[' cli_pub_port '] = cli_pub_add[1]    
           User_list.append (Now_user) toclient = ' info#%s login in successful, the info from server '%now_user[' user_name '] sock.sendto (Toclien T,cli_pub_add) print '-' *100 print '%s is already logged in, public network ip:%s port:%d\n "% (now_user[' user_name '],now_user[')    
                        Cli_pub_ip '],now_user[' cli_pub_port '] print "Below is the list of logged-in users for One_user in User_list: print ' Username:%s ip:%s public network port:%s private network ip:%s private network port:%s '% (one_user[' user_name '],one_user[' cli_pub_ip "],one_user    
            [' Cli_pub_port '],one_user[' private_ip '],one_user[' private_port '] elif cli_str[' type '] = = ' Alive ': Pass elif cli_str[' type '] = = ' logout ': Pass Elif    
                        cli_str[' type '] = = ' Getalluser ': print '-' *100 for One_user in user_list: toclient = ' getalluser#username:%s pub_ip:%s pub_port:%s pri_ip:%s '% (pri_port:%s ' one_user[' user_name ' Cli_pub_ip '],one_user['Cli_pub_port '],one_user[' private_ip '],one_user[' Private_port '] sock.sendto (toclient,cli_pub_a
    DD) If __name__ = = ' __main__ ': Thread.start_new_thread (Server_handle, ()) print ' server process started, waiting for client to connect ' While true:for one_user into user_list:toclient = ' keepconnect#111 ' Sock.sendto (to Client, (one_user[' cli_pub_ip '],one_user[' Cli_pub_port ')) Time.sleep (1)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.