This article mainly introduces how to compile a CS network program in python3, which has some reference value for those who learn Python network program design, for more information about how to compile a C/S network program in python3, see the following example. The specific method is as follows:
The example described in this article is a C/S applet compiled according to wingIDE prompts. The specific code is as follows:
The client myclient. py code is as follows:
#! /Bin/env python #-*-coding: gb18030-*-# import socket import timei = 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 () # note that in python3.0, network transmission must be in the 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 myserver. py code on the server side 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(10)while True: cfd,address=s.accept() buf=cfd.recv(1024) print(buf,address) cfd.send(buf) cfd.close()
I hope the examples in this article can provide some reference for everyone's Python network program design.