IoT PoC项目中需要展示视频采集源进行wifi切换后(表明视频采集源端发生了移动),接收端观看到的视频的流畅度,以及当接收端进行移动时,检测视频的流畅度,故需要一个模块周期性地探测本地IP是否变化,以指示设备的移动性。
操作:当设备做为视频接受者发生移动时,在IP变化后立刻向视频发送者发送携带有新IP的内容请求消息
代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
#!/usr/bin/env python # -*- coding: utf-8 -*- import time import subprocess from time import sleep import logging.config from socket import * import binascii import json import re def search(pattern,text,flag): #print(text) m = re.search(pattern,text) if m is not None : return m.group(flag) def get_ip(cmd): ip_cur = '0.0.0.0' while ip_cur = = '0.0.0.0' : obj = subprocess.Popen(cmd,stdout = subprocess.PIPE,stderr = subprocess.PIPE,shell = True ) obj.wait() lines = obj.stdout.readlines() for eachline in lines: eachline = eachline.decode( 'gbk' ) #print(eachline) strs = 'IPv4 地址' if strs in eachline: content = search( 'IPv4 地址 . . . . . . . . . . . . :(.*?)\r\n' ,eachline, 1 ) #print(content) ip_cur = str (content) print (ip_cur) break return ip_cur def send_data_request(S_EUID,D_EUID,C_EUID,NA,port): address = (NA, port) s = socket(AF_INET,SOCK_DGRAM) flag = 1 while flag: s_EUID = binascii.a2b_hex(S_EUID) d_EUID = binascii.a2b_hex(D_EUID) sev_type = binascii.a2b_hex( hex ( 22 ).replace( '0x' ,'')) Head_len = binascii.a2b_hex( hex ( 36 ).replace( '0x' ,'')) ICN_check = binascii.a2b_hex( hex ( 4444 ).replace( '0x' ,'')) packet_type = binascii.a2b_hex( '0' + hex ( 1 ).replace( '0x' ,'')) c_EUID = binascii.a2b_hex(C_EUID) data = s_EUID + d_EUID + sev_type + Head_len\ + ICN_check + packet_type + c_EUID print ( len (data)) if not data: break s.sendto(data,address) flag = 0 s.close() def ip_cmp(cmd,S_EUID,D_EUID,C_EUID,NA,port,IP_cur,get_cnt): ip_next = get_ip(cmd) get_cnt = get_cnt + 1 if time_flag: if ip_cur! = '0.0.0.0' and ip_next ! = '0.0.0.0' : if ip_cur = = ip_next: change_flag = 0 print ( 'change_flag=' + str (change_flag)) else : change_flag = 1 print ( 'change_flag=' + str (change_flag)) send_data_request(S_EUID,D_EUID,C_EUID,NA,port) return ip_next if __name__ = = '__main__' : cmd = 'ipconfig' get_cnt = 0 #ip 获取次数计数器 T_interval = 0.1 #ip 获取周期 ip_cur = '0.0.0.0' ip_next = '0.0.0.0' ip_cur = get_ip(cmd) #ip_cur='192.168.110.1' get_cnt = get_cnt + 1 time_flag = 1 S_EUID = 'aaaa' D_EUID = 'bbbb' C_EUID = 'cccc' NA = '192.168.1.100' port = 35000 while time_flag: ip_next = ip_cmp(cmd,S_EUID,D_EUID,C_EUID,NA,port,ip_cur,get_cnt) print ( 'IP_next=' + ip_next) sleep(T_interval) ip_cur = ip_next print ( 'IP_cur=' + ip_next) |
本代码中,利用ipconfig指令获取IP地址,该方法可能存在一定的问题,故还可采用其他的方法获取IP,如用socket方法可以获取本地的外网IP,这里不赘述,可再百度相关的方法。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/lynnucas/article/details/78289761