Python3 programming C/S network program instance tutorial, python3 instance tutorial
This article describes how to compile a C/S network program in python3 as an 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.
A) Write a network program, which must communicate in C/S mode. The client issues commands and the server responds to the client and the server program using C
Check the socket communication, and then you need to check the basic knowledge of VC. On the two interfaces, the client sends data and the server returns the response to the client.
Write a small program in python
Def input_1 ():
A = raw_input ('Please input something: \ n ')
Global st
St = list ()
Print 'Now the list you just inputted is: \ n', st
Success = True
While success:
Input_1 ()
For I in st:
C = st. count (I)
If c> = 3:
Print 'you lost! '
Print 'error: The number of % s you just input is % s' % (I, c)
Success = True
Break
Print 'the number of % s you inputted is % s time (s) '% (I, c)
Success = False
Print 'success! '