Python implements simple point-to-point (p2p) chat and pythonp2p
Point-to-Point chat is a multi-thread-based network programming. Secondly, each connection is saved as an object with unique attributes and added to the connection list, the information sent from each connection object must contain the main three items (from, to, messages ), in this way, after the information is sent to the server, the server will find the target object and send the information to the target object based on the connected object traversal connection list. After the target obtains the information, it will know who sent the information, then, reply according to the id number. This implementation will continue to be improved, and the new functions will be displayed on my github homepage in the future.
Server implementation:
# Coding: UTF-8 ''' file: server. pydate: Drawing /9/10 12: 43 author: lockeyemail: lockey@123.complatform: win7.x86 _ 64 pycharm python3desc: p2p communication serverside ''' import socketserver, jsonimport subprocessconnLst = [] # connection list, class Connector (object): # connection object class def _ init _ (self, account, password, addrPort, conObj): self. account = account self. password = password self. addrPort = addrPort se Lf. conObj = conObjclass MyServer (socketserver. baseRequestHandler): def handle (self): print ("got connection from", self. client_address) register = False while True: conn = self. request data = conn. recv (1024) if not data: continue dataobj = json. loads (data. decode ('utf-8') # if the format of the information sent from the Connection Client is a list and the registration ID is False, the user registers if type (dataobj) = list and not register: account = dataobj [0] password = dataobj [1] co NObj = Connector (account, password, self. client_address, self. request) connLst. append (conObj) register = True continue print (connLst) # if the target client sends data to the target client if len (connLst)> 1 and type (dataobj) = dict: sendok = False for obj in connLst: if dataobj ['to'] = obj. account: obj. conObj. sendall (data) sendok = True if sendok = False: print ('no target valid! ') Else: conn. sendall ('Nobody recevied! '. Encode ('utf-8') continueif _ name _ = '_ main _': server = socketserver. threadingTCPServer ('2017. 168.1.4 ', 8022), MyServer) print ('Waiting for connection... ') server. serve_forever ()
Client implementation:
# Coding: UTF-8 ''' file: client. py. pydate: 11: 01 author: lockeyemail: lockey@123.complatform: win7.x86 _ 64 pycharm python3desc: p2p communication clientside ''' from socket import * import threading, sys, json, reHOST = '192. 168.1.4 '# PORT = 8022 BUFSIZ = 1024 # buffer size 1 KADDR = (HOST, PORT) tcpCliSock = socket (AF_INET, SOCK_STREAM) tcpCliSock. connect (ADDR) userAccount = Nonedef register (): myre = r "^ [_ a-zA-Z] \ w {0 ,} "# Regular Expression verification username compliance accout = input ('Please input your account: ') if not re. findall (myre, accout): print ('account illegal! ') Return None password1 = input ('Please input your password:') password2 = input ('Please confirm your password: ') if not (password1 and password1 = password2 ): print ('password not illegal! ') Return None global userAccount = accout return (accout, password1) class inputdata (threading. thread): def run (self): while True: sendto = input ('to >>:') msg = input ('msg >> :') dataObj = {'to': sendto, 'msg ': msg, 'from': userAccount} datastr = json. dumps (dataObj) tcpCliSock. send (datastr. encode ('utf-8') class getdata (threading. thread): def run (self): while True: data = tcpCliSock. recv (BUFSIZ) dataObj = json. loads (data. decode ('utf-8') print ('{}-> {}'. format (dataObj ['from'], dataObj ['msg ']) def main (): while True: regInfo = register () if regInfo: datastr = json. dumps (regInfo) tcpCliSock. send (datastr. encode ('utf-8') break myinputd = inputdata () mygetdata = getdata () myinputd. start () mygetdata. start () myinputd. join () mygetdata. join () if _ name _ = '_ main _': main ()
Running result example:
Server result:
Client 1:
Client 2:
Client 3:
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.