python网络编程实例简析

yipeiwu_com6年前Python基础

本文实例讲述了python网络编程,分享给大家供大家参考。

具体方法如下:

服务端代码如下:

from SocketServer import(TCPServer as TCP, 
             StreamRequestHandler as SRH) 
from time import ctime 
 
HOST = '' 
PORT = 21567 
ADDR = (HOST, PORT) 
class MyRequestHandle(SRH): 
  def handle(self): 
    print 'connecting from ..', self.client_address 
    self.wfile.write("[%s]:%s" %  
             (ctime(),self.rfile.readline()) 
             ) 
tcp_Server = TCP(ADDR,MyRequestHandle) 
print 'WAITING connecting...' 
tcp_Server.serve_forever() 

客户端代码如下:

from socket import * 
 
HOST = 'localhost' 
PORT = 21567 
BUFSIZE = 1024 
ADDR = (HOST, PORT) 
 
while True: 
  tcpCliSock = socket(AF_INET,SOCK_STREAM) 
  tcpCliSock.connect(ADDR) 
  data = raw_input('>>>') 
  if not data: 
    break 
  tcpCliSock.send("%s\r\n" % data) 
  data = tcpCliSock.recv(BUFSIZE) 
  if not data: 
    break 
  print data.strip() 
  tcpCliSock.close() 

希望本文所述对大家的Python程序设计有所帮助。

相关文章

解决Python下imread,imwrite不支持中文的问题

由以下函数代替该功能: def cv_imread(file_path): cv_img=cv2.imdecode(np.fromfile(file_path,dtype=np.u...

python中pygame模块用法实例

python中pygame模块用法实例

本文实例讲述了python中pygame模块用法,分享给大家供大家参考。具体方法如下: import pygame, sys from pygame.locals import *...

Python图形绘制操作之正弦曲线实现方法分析

Python图形绘制操作之正弦曲线实现方法分析

本文实例讲述了Python图形绘制操作之正弦曲线实现方法。分享给大家供大家参考,具体如下: 要画正弦曲线先设定一下x的取值范围,从0到2π。要用到numpy模块。 numpy.pi 表示...

python实现指定字符串补全空格的方法

本文实例讲述了python实现指定字符串补全空格的方法。分享给大家供大家参考。具体分析如下: 如果希望字符串的长度固定,给定的字符串又不够长度,我们可以通过rjust,ljust和cen...

详解Python中的循环语句的用法

一、简介       Python的条件和循环语句,决定了程序的控制流程,体现结构的多样性。须重要理解,if、while、for以及与它...