This paper describes the implementation method of C/S network program written by Python3. Here's how:
The example described in this article is a C/s applet written according to Wingide, the specific code is as follows:
The client side myclient.py code is as follows:
#!/bin/env python#-*-coding:gb18030-*-#import socket import timei=1while i<10: address= ("127.0.0.1", 3138) S=socket.socket (socket.af_inet, socket. SOCK_STREAM) s.connect (address) buf= ' n:%d '% i s.send (Buf.encode ()) #注意, In python3.0, the network send must be in byte string format, such as S.send (b "ABC") Buff=s.recv (()) if (len): print (buff) S.close time.sleep (1) i+=1
The server-side myserver.py code is as follows:
#!/bin/env python#-*-coding:gb18030-*-#import socketaddress= (' 127.0.0.1 ', 3138) s=socket.socket (Socket.af_inet, Socket. SOCK_STREAM) S.bind (address) S.listen (Ten) while True: cfd,address=s.accept () buf=cfd.recv (1024x768) Print (buf,address) cfd.send (BUF) cfd.close ()
It is hoped that the example of this paper will have some reference for Python network program design.