Python实现基于TCP UDP协议的IPv4 IPv6模式客户端和服务端功能示例


Posted in Python onMarch 22, 2018

本文实例讲述了Python实现基于TCP UDP协议的IPv4 IPv6模式客户端和服务端功能。分享给大家供大家参考,具体如下:

由于目前工作的需要,需要在IPv4和IPv6两种网络模式下TCP和UDP的连接,要做到客户端发包,服务端收包。

前几天写了代码,但是把UDP的客户端和服务端使用TCP模式的代码了。今天在公司使用该工具的时候,发现了问题,忘记了UDP不需要验证。疏忽,疏忽。不过刚刚接触编程,可以原谅。

现在在家,已经把代码改好了。经测试可以使用。

先运行客户端:

python MiniClient.py host port mode(t4, t6, u4, u6)

再运行服务端:

python MiniServer.py host port mode(t4, t6, u4, u6)

客户端代码如下:

import socket, sys
import time
class MiniClient:
  h = ''
  p = ''
  m = ''
  def __init__(self, host, port, mode):
    self.h = host
    self.p = int(port)
    self.m = mode
  def tcpC4(self):
    tcpT4Client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    print "Done........"
    tcpT4Client.connect((self.h, self.p))
    print "TCP IPv4 TCP mode connecting..."
    while True:
      time.sleep(1)
      tcpT4Client.send('hello')
      print "hello send to Server"
  def udpC4(self):
    udpT4Client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    print "UDP TCP IPv4 Mode connecting..."
    while True:
      time.sleep(1)
      udpT4Client.sendto("hello", (self.h, self.p))
      print "Hello Send to " , self.h , ' Use ', self.p, 'Port'
  def tcpC6(self):
    tcpT4Client = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
    print "Done........"
    tcpT4Client.connect((self.h, self.p))
    print "TCP IPv6 TCP mode connecting..."
    while True:
      time.sleep(1)
      tcpT4Client.send('hello')
      print "hello send to Server"
  def udpC6(self):
    udpU6Client = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
    print "UDP TCP IPv4 Mode connecting..."
    while True:
      time.sleep(1)
      udpU6Client.sendto("hello", (self.h, self.p))
      print "Hello Send to " , self.h , ' Use ', self.p, 'Port'
if __name__ == "__main__":
  x = MiniClient(sys.argv[1], sys.argv[2], sys.argv[3])
  if x.m == 't4':
    x.tcpC4()
  elif x.m == 't6':
    x.tcpC6()
  elif x.m == 'u4':
    x.udpC4()
  else:
    x.udpC6()

服务端代码:

import socket, sys
class MiniServer:
  h = ''
  p = ''
  m = ''
  def __init__(self, host, port, mode):
    self.h = host
    self.p = int(port)
    self.m = mode
  def serverT4(self):
    tcpT4Server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    print "Server Socket Created......."
    tcpT4Server.bind((self.h, self.p))
    print "Wating for connecting......."
    tcpT4Server.listen(5)
    while True:
      clientSock, clientaddr = tcpT4Server.accept()
      print "Connected from: ", clientSock.getpeername()
      clientSock.send('Congratulations........')
      while True:
        buf = clientSock.recv(1024)
        print buf
      #clientSock.close()
  def udpT4(self):
    udpT4Server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    print "UDP TCP IPv4 Mode Start....."
    udpT4Server.bind((self.h, self.p))
    print "UDP Server Start"
    while True:
      udpT4Data, udpT4ServerInfo = udpT4Server.recvfrom(1024)
      print "Receive from ", udpT4ServerInfo, " and The Data send from The Client is :", udpT4Data
  def serverT6(self):
    tcpT6Server = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
    print "Server Socket Created......."
    tcpT6Server.bind((self.h, self.p))
    print "Wating for connecting......."
    tcpT6Server.listen(5)
    while True:
      clientSock, clientaddr = tcpT6Server.accept()
      print "Connected from: ", clientSock.getpeername()
      clientSock.send('Congratulations........')
      #clientSock.close()
  def udpT6(self):
    udpT6Server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    print "UDP TCP IPv4 Mode Start....."
    udpT6Server.bind((self.h, self.p))
    print "UDP Server Start"
    while True:
      udpT4Data, udpT6ServerInfo = udpT6Server.recvfrom(1024)
      print "Receive from ", udpT6ServerInfo, " and The Data send from The Client is :", udpT4Data
if __name__ == "__main__":
  x = MiniServer(sys.argv[1], sys.argv[2], sys.argv[3])
  if x.m == 't4':
    x.serverT4()
  elif x.m == 't6':
    x.serverT6()
  elif x.m == 'u4':
    x.udpT4()
  else:
    x.udpT6()

更多关于Python相关内容可查看本站专题:《Python Socket编程技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》

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

Python 相关文章推荐
Python ORM框架SQLAlchemy学习笔记之关系映射实例
Jun 10 Python
python与php实现分割文件代码
Mar 06 Python
Python 逐行分割大txt文件的方法
Oct 10 Python
详解python里使用正则表达式的全匹配功能
Oct 19 Python
python中hashlib模块用法示例
Oct 30 Python
详解Python中where()函数的用法
Mar 27 Python
对Pyhon实现静态变量全局变量的方法详解
Jan 11 Python
Django框架验证码用法实例分析
May 10 Python
Selenium+Python 自动化操控登录界面实例(有简单验证码图片校验)
Jun 28 Python
Python参数类型以及常见的坑详解
Jul 08 Python
Python中常见的数制转换有哪些
May 27 Python
python+requests接口自动化框架的实现
Aug 31 Python
Python cookbook(数据结构与算法)将名称映射到序列元素中的方法
Mar 22 #Python
Python cookbook(数据结构与算法)从字典中提取子集的方法示例
Mar 22 #Python
python实现将excel文件转化成CSV格式
Mar 22 #Python
python 对象和json互相转换方法
Mar 22 #Python
利用python将json数据转换为csv格式的方法
Mar 22 #Python
解决python3中解压zip文件是文件名乱码的问题
Mar 22 #Python
Python爬虫工程师面试问题总结
Mar 22 #Python
You might like
PHP的ASP防火墙
2006/10/09 PHP
php去掉URL网址中带有PHPSESSID的配置方法
2014/07/08 PHP
PHP对象递归引用造成内存泄漏分析
2014/08/28 PHP
PHP中使用substr()截取字符串出现中文乱码问题该怎么办
2015/10/21 PHP
php实现的简单中文验证码功能示例
2017/01/03 PHP
php设计模式之装饰模式应用案例详解
2019/06/17 PHP
脚本吧 - 幻宇工作室用到js,超强推荐base.js
2006/12/23 Javascript
浅谈javascript的原型继承
2012/07/25 Javascript
jquery 跳到顶部和底部动画2句代码简单实现
2013/07/18 Javascript
javascript 按键事件(兼容各浏览器)
2013/12/20 Javascript
JavaScript中switch语句的用法详解
2015/06/03 Javascript
详解javascript实现自定义事件
2016/01/19 Javascript
详解Angular.js的$q.defer()服务异步处理
2016/11/06 Javascript
Javascript 制作图形验证码实例详解
2016/12/22 Javascript
Omi v1.0.2发布正式支持传递javascript表达式
2017/03/21 Javascript
[js高手之路]图解javascript的原型(prototype)对象,原型链实例
2017/08/28 Javascript
swiper插件自定义切换箭头按钮
2017/12/28 Javascript
基于Vuejs的搜索匹配功能实现方法
2018/03/03 Javascript
小程序点击图片实现png转jpg
2019/10/22 Javascript
nodeJS与MySQL实现分页数据以及倒序数据
2020/06/05 NodeJs
js实现限定区域范围拖拉拽效果
2020/11/20 Javascript
Python wxPython库消息对话框MessageDialog用法示例
2018/09/03 Python
Django windows使用Apache实现部署流程解析
2020/10/12 Python
深入解析HTML5的IndexedDB索引数据库
2015/09/14 HTML / CSS
阿里巴巴英国:Alibaba英国
2019/12/11 全球购物
英国时尚高尔夫服装购物网站:Trendy Golf
2020/01/10 全球购物
Ibatis中如何提高SQL Map的性能
2013/05/11 面试题
员工培训心得体会
2013/12/30 职场文书
大学生两会学习心得体会
2014/03/10 职场文书
党校毕业心得体会
2014/09/13 职场文书
2014年房产经纪人工作总结
2014/12/08 职场文书
学雷锋日活动总结
2015/02/06 职场文书
慰问信模板
2015/02/14 职场文书
Python 多线程之threading 模块的使用
2021/04/14 Python
基于Python绘制子图及子图刻度的变换等的问题
2021/05/23 Python
Nginx防盗链与服务优化配置的全过程
2022/01/18 Servers