This paper describes the implementation method of Python3 programming C/S network program in the form of an example. The specific methods are as follows:
The example described in this article is a C/s applet written according to Wingide's prompts, the specific code is as follows:
The client-side myclient.py code is as follows:
#!/bin/env python
#-*-coding:gb18030-*-
#
Import socket
import time
i=1 while
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, a network send must be in a byte string format, such as S.send (b "ABC")
buff=s.recv (1024)
if (len (Buff)):
print (Buff)
s.close
time.sleep (1)
i+=1
The server-side myserver.py code is as follows:
#!/bin/env python
#-*-coding:gb18030-*-
#
Import socket
address= (' 127.0.0.1 ', 3138)
s= Socket.socket (socket.af_inet, socket. SOCK_STREAM)
S.bind (address)
S.listen (a) while
True:
cfd,address=s.accept ()
buf= CFD.RECV (1024)
print (buf,address)
cfd.send (BUF)
cfd.close ()
I hope this example of Python network program design can have a certain reference role.