Python3 writing c/S Network program Example tutorial

Source: Internet
Author: User
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.

  • 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.