Python 之自动获取公网IP

上传人:我*** 文档编号:135970162 上传时间:2020-06-21 格式:DOC 页数:12 大小:131.50KB
返回 下载 相关 举报
Python 之自动获取公网IP_第1页
第1页 / 共12页
Python 之自动获取公网IP_第2页
第2页 / 共12页
Python 之自动获取公网IP_第3页
第3页 / 共12页
Python 之自动获取公网IP_第4页
第4页 / 共12页
Python 之自动获取公网IP_第5页
第5页 / 共12页
点击查看更多>>
资源描述

《Python 之自动获取公网IP》由会员分享,可在线阅读,更多相关《Python 之自动获取公网IP(12页珍藏版)》请在金锄头文库上搜索。

1、Python 之自动获取公网IP2017年9月30日0. 预备知识0.1 SQL基础ubuntu、Debian系列安装:rootraspberrypi:/python-script# apt-get install mysql-server Redhat、Centos 系列安装:rootlocalhost # yum install mysql-server登录数据库piraspberrypi: $ mysql -uroot -p -hlocalhostEnter password: Welcome to the MariaDB monitor. Commands end with ; or

2、g.Your MariaDB connection id is 36Server version: 10.0.30-MariaDB-0+deb8u2 (Raspbian)Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.Type help; or h for help. Type c to clear the current input statement.MariaDB (none) 其中,mysql是客户端命令 -u是指定用户 -p是密码 -h是主机创建数据库、创建数据表创建数据库语法如下MariaDB

3、(none) help create databaseName: CREATE DATABASEDescription:Syntax:CREATE DATABASE | SCHEMA IF NOT EXISTS db_name create_specification .create_specification: DEFAULT CHARACTER SET = charset_name | DEFAULT COLLATE = collation_nameCREATE DATABASE creates a database with the given name. To use thisstat

4、ement, you need the CREATE privilege for the database. CREATESCHEMA is a synonym for CREATE DATABASE.URL: https:/ (none) 创建数据表语法如下MariaDB (none) help create tableName: CREATE TABLEDescription:Syntax:CREATE TEMPORARY TABLE IF NOT EXISTS tbl_name (create_definition,.) table_options partition_optionsOr

5、:CREATE TEMPORARY TABLE IF NOT EXISTS tbl_name (create_definition,.) table_options partition_options select_statement创建数据库ServiceLogsMariaDB (none) CREATE DATABASE ServiceLogs创建数据表MariaDB (none) CREATE TABLE python_ip_logs ( serial_number bigint(20) NOT NULL AUTO_INCREMENT, time datetime DEFAULT NUL

6、L, old_data varchar(50) DEFAULT NULL, new_data varchar(50) DEFAULT NULL, PRIMARY KEY (serial_number) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1 表内容的查询MariaDB ServiceLogs select * from python_ip_logs;Empty set (0.00 sec)0.2 python连接操作MySQL模块下载安装下载路径: https:/pypi.python.org/pypi/MySQL-pytho

7、n安装:解压unzip MySQL-python-1.2.5.zip进入解压后目录cd MySQL-python-1.2.5/安装依赖apt-get install libmysqlclient-dev安装python setup.py install如果为0则安装OKecho $?连接Mysqlrootraspberrypi:/python-script# cat p_mysql_3.py #!/usr/bin/env pythonimport MySQLdbtry : conn = MySQLdb.connect(主机,用户名,密码,ServiceLogs) print (Connect

8、Mysql successful)except: print (Connect MySQL Fail)rootraspberrypi:/python-script# 如果提示Connect Mysql successful则说明连接OKPython MySQL insert语句rootraspberrypi:/python-script# cat p_mysql1.py #!/usr/bin/env pythonimport MySQLdbdb = MySQLdb.connect(localhost,root,root,ServiceLogs)cursor = db.cursor()sql =

9、 insert INTO python_ip_logs VALUES (DEFAULT,2017-09-29 22:46:00,123,456)cursor.execute(sql)mit()db.close()rootraspberrypi:/python-script# 执行完成后可以查看得知是否插入成功1. 需求1.1 需求由于宽带每次重启都会重新获得一个新的IP,那么在这种状态下,在进行ssh连接的时候会出现诸多的不便,好在之前还有花生壳软件,它能够通过域名来找到你的IP地址,进行访问,这样是最好的,不过最近花生壳也要进行实名认证才能够使用,于是乎,这就催发了我写一个python脚本来

10、获取公网IP的冲动。实现效果:当IP变更时,能够通过邮件进行通知,且在数据库中写入数据1.2 大致思路1.3 流程图其他代码均没有什么好画的,故就没有画2. 代码编写2.1.1 编写python代码getnetworkip.pyrootraspberrypi:/python-script# cat getnetworkip.py #!/usr/bin/env python# coding:UTF-8import requestsimport send_mailimport savedbdef get_out_ip() : url = rhttp:/ r = requests.get(url)

11、txt = r.text ip = txttxt.find(title)+6:txt.find(/title)-1 return (ip)def main() : try: savedb.general_files() tip = get_out_ip() cip = savedb.read_files() if savedb.write_files(cip,tip) : send_mail.SamMail(get_out_ip() except : return Falseif _name_=_main_ : main()rootraspberrypi:/python-script# sav

12、edb.pyrootraspberrypi:/python-script# cat savedb.py#!/usr/bin/env pythonimport MySQLdbimport osimport timedirname = logsfilename = logs/.ip_tmpdef general_files(Default_String=Null) : var1 = Default_String if not os.path.exists(dirname) : os.makedirs(dirname) if not os.path.exists(filename) : f = op

13、en(filename,w) f.write(var1) f.close()def read_files() : f = open(filename,r) txt = f.readline() return (txt)def write_files(txt,new_ip) : if not txt = new_ip : NowTime = time.strftime(%Y-%m-%d %H:%M:%S, time.localtime() old_ip = read_files() os.remove(filename) general_files(new_ip) write_db(NowTime,old_ip,new_ip) return True

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 事务文书

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号