Python3 programming C/S network program instance tutorial, python3 instance tutorial

Source: Internet
Author: User

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! '




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.