国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python實現基于TCP UDP協議的IPv4 IPv6模式客戶端和服務端功能示例

2020-02-22 23:31:26
字體:
來源:轉載
供稿:網友

本文實例講述了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, sysimport timeclass 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, sysclass 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 :", udpT4Dataif __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()            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 旌德县| 江华| 巴里| 包头市| 新巴尔虎左旗| 龙南县| 芒康县| 陇西县| 白水县| 奈曼旗| 专栏| 兴国县| 灵台县| 广宗县| 荔浦县| 蛟河市| 嘉峪关市| 白玉县| 西乌珠穆沁旗| 柳江县| 临夏县| 平凉市| 磴口县| 遂平县| 浪卡子县| 舟曲县| 沅陵县| 金湖县| 杭锦后旗| 军事| 鹿邑县| 承德市| 宜良县| 沁水县| 两当县| 正定县| 长阳| 亚东县| 阳曲县| 元江| 阳新县|