Python 之自动获取公网IP_第1页
Python 之自动获取公网IP_第2页
Python 之自动获取公网IP_第3页
Python 之自动获取公网IP_第4页
Python 之自动获取公网IP_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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: /kb/en/create-database/MariaDB (none) 创建数据表语法如下MariaDB (none) help create tableName: CREATE TABLEDescription:Syntax:CREATE TEMPORARY TABLE IF NOT EXISTS tbl_name (create_defini

5、tion,.) table_options partition_optionsOr: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

6、AUTO_INCREMENT, time datetime DEFAULT NULL, 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模块下载安装下载路径

7、: /pypi/MySQL-python安装:解压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.conne

8、ct(主机,用户名,密码,ServiceLogs) print (Connect 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,r

9、oot,ServiceLogs)cursor = db.cursor()sql = 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地址,进行访问,这样是

10、最好的,不过最近花生壳也要进行实名认证才能够使用,于是乎,这就催发了我写一个python脚本来获取公网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_

11、out_ip() : url = r/ r = requests.get(url) 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 Fa

12、lseif _name_=_main_ : main()rootraspberrypi:/python-script# savedb.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)

13、: os.makedirs(dirname) if not os.path.exists(filename) : f = open(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_fi

14、les() os.remove(filename) general_files(new_ip) write_db(NowTime,old_ip,new_ip) return True else: return Falsedef write_db(NowTime,Old_ip,New_ip) : db = MySQLdb.connect(主机,用户名,密码,库名) cursor = db.cursor() sql = INSERT INTO python_ip_logs VALUES (DEFAULT,%s,%s,%s) %(NowTime,Old_ip,New_ip) try: cursor.

15、execute(sql) mit() except: db.rollback() db.close()rootraspberrypi:/python-script# send_mail.pyrootraspberrypi:/python-script# cat send_mail.py#!/usr/bin/env pythonimport smtplibimport email.mime.textdef SamMail(HtmlString) : HOST = SUBJECT = 主题 TO = 对方的邮箱地址 FROM = 来自于哪里 Remask = The IP address has been changed msg = email.mime.text.MIMEText( ip:%s %(HtmlString),html,utf-8) msgSubject = SUBJECT msgFrom = FROM msgTO = TO try: serve

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论