【Python】簡單的UDP通訊介面
阿新 • • 發佈:2018-12-23
(一)建立介面
wxFronBuilder
使用方法,在TCP通訊詳細介紹過,大致如圖:
建立完成,選中專案->code選擇Python
儲存->點選F8獲得 noname.py
noname.py 部分截圖:
(二)建立UdpChat專案
eclipse
建立udpChat專案,並把 noname.py 引入貼上就行
:
注:noname.py 原資料夾的位置要引入到eclipse專案中,引入方法在TCP聊天中也說過,我依然放在wxFrom目錄下,這裡跳過引入步驟
(三)一個簡單的Demo
具體的UDP通訊方法,在程式碼註釋中有講解
1、開啟_init _.py
原始碼:
_init _.py
# udpclient
import wx
import noname
import socket
import threading
udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # 建立套接字
class ClientFrame(noname.MyFrame1):
def __init__(self, parent):
noname.MyFrame1.__init__(self, parent)
def btn_login (self, event):
ipStr = self.ip_text.GetValue()
portStr = int(self.port_text.GetValue())
# 建立udp套接字進行連線
udp_socket.bind((ipStr, portStr)) # 伺服器繫結ip和埠
self.showmessage.AppendText('----已上線!----\n') # 顯示連線成功
def btn_send(self, event):
# while True:
send_ip = self.send_ip.GetValue()
send_port = int(self.send_port.GetValue())
send_address = (send_ip, send_port) # 傳送地址
send_msg = self.sendmessage.GetValue() # 獲得傳送內容
send_data = send_msg.encode('utf-8') # 轉碼
self.showmessage.AppendText('\n 說:%s' % send_msg)
# sendto(傳送資料,傳送地址)
udp_socket.sendto(send_data, send_address)
self.sendmessage.SetValue("") # 清空輸入欄
# 接收資訊
recv_data = udp_socket.recv(1024)
recv_data = recv_data.decode('utf-8')
self.showmessage.AppendText('\n 收到資訊為:%s' % recv_data)
print('收到資訊為:%s' % recv_data)
def btn_out(self, event):
self.Close(True) # 關閉視窗
def main():
app = wx.App(False)
frame = ClientFrame(None)
frame.Show(True)
# 啟動視窗
app.MainLoop()
if __name__ == "__main__":
try:
main()
finally:
udp_socket.close()
3、執行結果:
執行
兩次
client可以對話,不過是sendto直接傳送訊息,沒有通過server,本來想通過server轉播的,讓server可以檢視所有人的對話,不過沒成功!emm
還有我在點擊發送後才能收到,簡單聊天如下:
附:
我的參考 noname.py:
# -*- coding: utf-8 -*-
###########################################################################
## Python code generated with wxFormBuilder (version Jan 23 2018)
## http://www.wxformbuilder.org/
##
## PLEASE DO *NOT* EDIT THIS FILE!
###########################################################################
import wx
import wx.xrc
###########################################################################
## Class MyFrame1
###########################################################################
class MyFrame1 ( wx.Frame ):
def __init__( self, parent ):
wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 619,466 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
self.SetSizeHints( wx.DefaultSize, wx.DefaultSize )
bSizer1 = wx.BoxSizer( wx.VERTICAL )
bSizer2 = wx.BoxSizer( wx.VERTICAL )
bSizer8 = wx.BoxSizer( wx.HORIZONTAL )
self.m_staticText1 = wx.StaticText( self, wx.ID_ANY, u"建立我的IP:", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText1.Wrap( -1 )
bSizer8.Add( self.m_staticText1, 0, wx.ALIGN_CENTER_VERTICAL|wx.TOP|wx.BOTTOM|wx.LEFT, 5 )
self.ip_text = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
bSizer8.Add( self.ip_text, 1, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5 )
self.m_staticText3 = wx.StaticText( self, wx.ID_ANY, u" Port:", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText3.Wrap( -1 )
bSizer8.Add( self.m_staticText3, 0, wx.ALIGN_CENTER_VERTICAL|wx.TOP|wx.BOTTOM|wx.LEFT, 5 )
self.port_text = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
bSizer8.Add( self.port_text, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
self.m_staticText7 = wx.StaticText( self, wx.ID_ANY, u"(1024~65535)", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText7.Wrap( -1 )
bSizer8.Add( self.m_staticText7, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5 )
bSizer8.Add( ( 0, 0), 1, wx.EXPAND, 5 )
self.enter_btn = wx.Button( self, wx.ID_ANY, u"進入", wx.DefaultPosition, wx.DefaultSize, 0 )
bSizer8.Add( self.enter_btn, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5 )
bSizer2.Add( bSizer8, 1, wx.EXPAND, 5 )
bSizer1.Add( bSizer2, 1, wx.EXPAND, 5 )
bSizer3 = wx.BoxSizer( wx.VERTICAL )
self.showmessage = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, wx.TE_MULTILINE )
bSizer3.Add( self.showmessage, 1, wx.ALL|wx.EXPAND, 5 )
bSizer1.Add( bSizer3, 3, wx.EXPAND, 5 )
bSizer4 = wx.BoxSizer( wx.VERTICAL )
bSizer81 = wx.BoxSizer( wx.HORIZONTAL )
self.m_staticText4 = wx.StaticText( self, wx.ID_ANY, u"接收者的ip:", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText4.Wrap( -1 )
bSizer81.Add( self.m_staticText4, 0, wx.ALL, 5 )
self.send_ip = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
bSizer81.Add( self.send_ip, 0, wx.ALL, 5 )
self.m_staticText6 = wx.StaticText( self, wx.ID_ANY, u"Port:", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText6.Wrap( -1 )
bSizer81.Add( self.m_staticText6, 0, wx.ALL, 5 )
self.send_port = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
bSizer81.Add( self.send_port, 0, wx.ALL, 5 )
self.m_staticText8 = wx.StaticText( self, wx.ID_ANY, u"(1024~65535)", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText8.Wrap( -1 )
bSizer81.Add( self.m_staticText8, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5 )
bSizer81.Add( ( 0, 0), 1, wx.EXPAND, 5 )
bSizer4.Add( bSizer81, 0, wx.EXPAND, 5 )
bSizer5 = wx.BoxSizer( wx.VERTICAL )
self.me = wx.StaticText( self, wx.ID_ANY, u"傳送的訊息:", wx.DefaultPosition, wx.DefaultSize, 0 )
self.me.Wrap( -1 )
bSizer5.Add( self.me, 0, wx.ALL, 5 )
self.sendmessage = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
bSizer5.Add( self.sendmessage, 1, wx.ALL|wx.EXPAND, 5 )
bSizer4.Add( bSizer5, 2, wx.EXPAND, 5 )
bSizer7 = wx.BoxSizer( wx.HORIZONTAL )
bSizer7.Add( ( 0, 0), 1, wx.EXPAND, 5 )
self.send_btn = wx.Button( self, wx.ID_ANY, u"傳送", wx.DefaultPosition, wx.DefaultSize, 0 )
bSizer7.Add( self.send_btn, 0, wx.ALL|wx.ALIGN_RIGHT, 5 )
self.exit_btn = wx.Button( self, wx.ID_ANY, u"退出", wx.DefaultPosition, wx.DefaultSize, 0 )
bSizer7.Add( self.exit_btn, 0, wx.ALL|wx.ALIGN_RIGHT, 5 )
bSizer4.Add( bSizer7, 0, wx.EXPAND|wx.ALIGN_CENTER_VERTICAL, 5 )
bSizer1.Add( bSizer4, 3, wx.EXPAND, 5 )
self.SetSizer( bSizer1 )
self.Layout()
self.Centre( wx.BOTH )
# Connect Events
self.enter_btn.Bind( wx.EVT_BUTTON, self.btn_login )
self.send_btn.Bind( wx.EVT_BUTTON, self.btn_send )
self.exit_btn.Bind( wx.EVT_BUTTON, self.btn_out )
def __del__( self ):
pass
# Virtual event handlers, overide them in your derived class
def btn_login( self, event ):
event.Skip()
def btn_send( self, event ):
event.Skip()
def btn_out( self, event ):
event.Skip()