一、UDP协议编程
(一)接收端代码
import socket s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.bind(('', 5000)) while True: data, addr = s.recvfrom(1024) data = data.decode() print('received message:{0} from PORT {1[1]} on {1[0]}'.format(data, addr)) if data.lower() == 'bye': break s.close( )
(二)发送端代码
import socket import sys s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.sendto(sys.argv[1].encode() , ("192.168.1.107" ,5000)) s.close( )
(三)方法
(四)运行效果截图
二、获取一个网页内容
import requests def getHTMLText(self): try: r = requests.get(url, timeout=30) r.raise_for_status() r.encoding = 'utf-8' return r.text except: return "" url = "http://www.baidu.com" print(getHTMLText(url))
运行效果截图: