1 socket套接字 class 对象
In [1]: import socket In [2]: help(socket.socket)
class socket(_socket.socket) | A subclass of _socket.socket adding the makefile() method. | | Method resolution order: | socket | _socket.socket | builtins.object | | Methods defined here: | | __enter__(self) | | __exit__(self, *args) | | __getstate__(self)
2.socket通信udp
from socket import * udp_socket = socket(AF_INET,SOCK_DGRAM) udp_socket.sendto("haha",("192.168.123.1",8080))
3.端口的问题
#-*- coding:utf-8 -*- from socket import * udp_socket = socket(AF_INET,SOCK_DGRAM) udp_socket.sendto("haha",("192.168.123.1",8080)) #使用upd发送的数据,在每一次都要写上接收方的ip和port udp_socket.sendto("haha",("192.168.123.1",8080))
2)绑定端口号
#-*- coding:utf-8 -*- from socket import * udp_socket = socket(AF_INET,SOCK_DGRAM) udp_socket.bind(("",8888)) udp_socket.sendto("haha",("192.168.123.1",8080))
4.接受数据
- 接收方需要绑定端口
- 发送方不需要绑定
#-*- coding:utf-8 -*- from socket import * udp_socket = socket(AF_INET,SOCK_DGRAM) udp_socket.bind(("",7777)) recv_data = udp_socket.recvfrom(1024) #套接字接受数据 print(recv_data)
5.upd网路通信过程