Home > Backend Development > Python Tutorial > python3编写C/S网络程序实例教程

python3编写C/S网络程序实例教程

WBOY
Release: 2016-06-06 11:32:38
Original
1411 people have browsed it

本文以实例形式讲述了python3编写C/S网络程序的实现方法。具体方法如下:

本文所述实例是根据wingIDE的提示编写的一个C/S小程序,具体代码如下:

client端myclient.py代码如下:

#!/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()) #注意,在python3.0中,网络发送必须采用字节字符串格式的,比如s.send(b"abc")
  buff=s.recv(1024)
  if(len(buff)):
    print(buff) 
  s.close
  time.sleep(1)
  i+=1

Copy after login

server端myserver.py代码如下:

#!/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(10)
while True:
  cfd,address=s.accept()
  buf=cfd.recv(1024)
  print(buf,address)
  cfd.send(buf)
  cfd.close()

Copy after login

希望本文实例对大家Python网络程序设计能有一定的参考借鉴作用。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template