日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python实现udp聊天室_python网络编程基础--socket的简介,以及使用socket来搭建一个简单的udp小程序...

發(fā)布時(shí)間:2025/3/15 python 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python实现udp聊天室_python网络编程基础--socket的简介,以及使用socket来搭建一个简单的udp小程序... 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

socket介紹:

socket(簡(jiǎn)稱套接字),是進(jìn)程間通訊的一個(gè)工具,他能實(shí)現(xiàn)把數(shù)據(jù)從一方傳輸?shù)搅硪环?#xff0c;完成不同電腦上進(jìn)程之間的通訊,它好比數(shù)據(jù)的搬運(yùn)工。

socket應(yīng)用:

不夸張來(lái)說(shuō),只要跟網(wǎng)絡(luò)相關(guān)的應(yīng)用程序或者軟件都使用到了socket, 比如:微信,qq等

使用socket創(chuàng)建udp網(wǎng)絡(luò)程序的流程:

1,創(chuàng)建客戶端套接字

2,發(fā)送/接收數(shù)據(jù)

3,關(guān)閉套接字

使用程序展現(xiàn)流程 :(使用的 ide為pycharm):

1,首先創(chuàng)建socket,在 Python 中 使用socket 模塊的函數(shù) socket 就可以完成:

import socket

2,創(chuàng)建udp socket套接字

創(chuàng)建套接字格式 :udp_socket = socket(AddressFamily, Type)

其中AddressFamily代表IP地址類型,AF_INET表示ipv4類型、AF_INET6表示ipv6類型; Type:套接字類型,可以是 SOCK_STREAM(流式套接字,主要用于 TCP 協(xié)議)或者 SOCK_DGRAM(數(shù)據(jù)報(bào)套接字,主要用于 UDP 協(xié)議)

udp_socket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)

3,發(fā)送/接收套接字(接上方的步驟)

首先準(zhǔn)備對(duì)方的 ip地址和 端口號(hào)

假設(shè)ip地址為:192.168.1.103

端口號(hào)為:8080

dest_addr = ('192.168.1.103',8080) #注意 是元組 ip地址為字符串,端口號(hào)為數(shù)字

從鍵盤獲取數(shù)據(jù)

send_data = input('請(qǐng)輸入要輸入的數(shù)據(jù) ')

發(fā)送數(shù)據(jù)到指定的電腦上

udp_socket = socket.sendto(send_data.encode('utf-8'),dest_addr)

#sendto是udp用來(lái)發(fā)送數(shù)據(jù)的,在發(fā)送的過(guò)程中我們需要對(duì)發(fā)送的數(shù)據(jù)進(jìn)行轉(zhuǎn)碼,轉(zhuǎn)換成utf-8

4,關(guān)閉套接字

udp_socket.close()

以上便是udp使用socket傳輸?shù)囊恍┗静襟E

整個(gè)發(fā)送/接受數(shù)據(jù)的小程序完整如下:

import socket

# 1. 創(chuàng)建udp套接字

udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# 2. 準(zhǔn)備接收方的地址

dest_addr = ('192.168.236.129', 8080)

# 3. 從鍵盤獲取數(shù)據(jù)

send_data = input("請(qǐng)輸入要發(fā)送的數(shù)據(jù):")

# 4. 發(fā)送數(shù)據(jù)到指定的電腦上

udp_socket.sendto(send_data.encode('utf-8'), dest_addr)

# 5. 等待接收對(duì)方發(fā)送的數(shù)據(jù)

recv_data = udp_socket.recvfrom(1024) # 1024表示本次接收的最大字節(jié)數(shù)

# 6. 顯示對(duì)方發(fā)送的數(shù)據(jù)

# 接收到的數(shù)據(jù)recv_data是一個(gè)元組

# 第1個(gè)元素是對(duì)方發(fā)送的數(shù)據(jù)

# 第2個(gè)元素是對(duì)方的ip和端口

print(recv_data[0].decode('gbk'))

print(recv_data[1])

# 7. 關(guān)閉套接字

udp_socket.close()

數(shù)據(jù)的編碼和解碼(簡(jiǎn)單兩行):

str->bytes:encode編碼

bytes->str:decode解碼

udp綁定端口:

用上方的小程序發(fā)送信息,我們每次發(fā)送的數(shù)據(jù),端口好都是會(huì)改變的,所以需要我們對(duì)端口進(jìn)行綁定

綁定示例:

from socket import *

# 1. 創(chuàng)建套接字

udp_socket = socket(AF_INET, SOCK_DGRAM)

# 2. 綁定本地的相關(guān)信息,如果一個(gè)網(wǎng)絡(luò)程序不綁定,則系統(tǒng)會(huì)隨機(jī)分配

local_addr = ('', 7788) # ip地址和端口號(hào),ip一般不用寫(xiě),表示本機(jī)的任何一個(gè)ip

udp_socket.bind(local_addr)

# 3. 等待接收對(duì)方發(fā)送的數(shù)據(jù)

recv_data = udp_socket.recvfrom(1024) # 1024表示本次接收的最大字節(jié)數(shù)

# 4. 顯示接收到的數(shù)據(jù)

print(recv_data[0].decode('gbk'))

# 5. 關(guān)閉套接字

udp_socket.close()

udp小程序的實(shí)戰(zhàn)(面向?qū)ο箝_(kāi)發(fā)):簡(jiǎn)單的聊天室(一起使用python的基礎(chǔ)知識(shí)和上訪的udp來(lái)實(shí)戰(zhàn)吧^_^)

import socket

#這個(gè)是發(fā)送信息的函數(shù)

def send_msg(udp_socket):

"""獲取鍵盤數(shù)據(jù),并將其發(fā)送給對(duì)方"""

# 1. 從鍵盤輸入數(shù)據(jù)

msg = input("\n請(qǐng)輸入要發(fā)送的數(shù)據(jù):")

# 2. 輸入對(duì)方的ip地址

dest_ip = input("\n請(qǐng)輸入對(duì)方的ip地址:")

# 3. 輸入對(duì)方的port

dest_port = int(input("\n請(qǐng)輸入對(duì)方的port:"))

# 4. 發(fā)送數(shù)據(jù)

udp_socket.sendto(msg.encode("utf-8"), (dest_ip, dest_port))

#這個(gè)是接受數(shù)據(jù)的函數(shù)

def recv_msg(udp_socket):

"""接收數(shù)據(jù)并顯示"""

# 1. 接收數(shù)據(jù)

recv_msg = udp_socket.recvfrom(1024)

# 2. 解碼

recv_ip = recv_msg[1]

recv_msg = recv_msg[0].decode("utf-8")

# 3. 顯示接收到的數(shù)據(jù)

print(">>>%s:%s" % (str(recv_ip), recv_msg))

#主函數(shù)

def main():

# 1. 創(chuàng)建套接字

udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# 2. 綁定本地信息

udp_socket.bind(("", 7890))

while True:

# 3. 選擇功能

print("="*30)

print("1:發(fā)送消息")

print("2:接收消息")

print("="*30)

op_num = input("請(qǐng)輸入要操作的功能序號(hào):")

# 4. 根據(jù)選擇調(diào)用相應(yīng)的函數(shù)

if op_num == "1":

send_msg(udp_socket)

elif op_num == "2":

recv_msg(udp_socket)

else:

print("輸入有誤,請(qǐng)重新輸入...")

if __name__ == "__main__":

main()

總結(jié)

以上是生活随笔為你收集整理的python实现udp聊天室_python网络编程基础--socket的简介,以及使用socket来搭建一个简单的udp小程序...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。