Python3 Programming Example Course of C/S network program _python

Source: Internet
Author: User

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.

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.