CTF(鶸)密码学

Python Algorithm CTF(鶸)密码学 一、摩斯密码 1、特点 题面只有三个值。 2、解题思路 转换成 ascii,出现 flag 标识符即结束,否则根据转后的数据进行下一步处理。 二、栅栏密码 1、特点 密文字符串出间隔性的出现 flag 的标识符。 2、解题思路 分栏破译。 def inputData(): string = input("请输入栅栏加密的文字:") code = input("请输入分栏:(0代表从2到6逐个分栏):") code = int(code) return string,code def code2(string): string_temp = [] string_temp.append(string[0::2]) string_temp.append(string[1::2]) print("分成2栏的结果是:%s" % (''.join(string_temp))) def code3(string): string_temp = [] string_temp.append(string[0::3]) string_temp.append(string[1::3]) string_temp.append(string[2::3]) print("分成3栏的结果是:%s" % (''.join(string_temp))) def code4(string): string_temp = [] string_temp.append(string[0::4]) string_temp.append(string[1::4]) string_temp.append(string[2::4]) string_temp.append(string[3::4]) print("分成4栏的结果是:%s" % (''.join(string_temp))) def code5(string): string_temp = [] string_temp.append(string[0::5]) string_temp.append(string[1::5]) string_temp.append(string[2::5]) string_temp.append(string[3::5]) string_temp.append(string[4::5]) print("分成5栏的结果是:%s" % (''.join(string_temp))) def code6(string): string_temp = [] string_temp.append(string[0::6]) string_temp.append(string[1::6]) string_temp.append(string[2::6]) string_temp.append(string[3::6]) string_temp.append(string[4::6]) string_temp.append(string[5::6]) print("分成6栏的结果是:%s" % (''.join(string_temp))) def main(): string, code = inputData() if (code == 0): code2(string) code3(string) code4(string) code5(string) code6(string) elif (code == 2): code2(string) elif (code == 3): code3(string) elif (code == 4): code4(string) elif (code == 5): code4(string) elif (code == 6): code4(string) else: print("error") if __name__ == "__main__": main() 三、Rot13 1、特点 凯撒加密的第十二种方式,但是可以字符和数字混合在其中。 ...

Deepin 切换壁纸小工具

Linux Python Deepin 切换壁纸小工具 切换壁纸这种事,找到接口,一行代码就可以解决,本来打算用 bash 脚本,但是考虑到随机选取壁纸等因素,用 python 的 os 模块完成任务。 一、思路 找到切换壁纸的接口 设置壁纸库(文件夹) python 脚本完成功能 半小时触发脚本自动切换壁纸 二、实现过程 1、切换壁纸接口 本脚本只在 Linux 下使用。 在 Linux 下(准确说是 GNOME 桌面),通过 gsettings set 指令完成对特定值(壁纸文件位置)进行修改,即可修改壁纸。 # Ubuntu 下切换指令(注意有个 file) gsettings set org.gnome.desktop.background picture-uri "file:/home/user/Desktop/1.jpg" #Deepin 下切换指令(注意没有 file) gsettings set com.deepin.wrap.gnome.desktop.background picture-uri "/home/user/Desktop/1.jpg" 2、设置壁纸库 这步没什么好说的啊,把喜欢的壁纸放在一个文件夹下,方便之后的存取使用。 3、python 完成功能 直接上代码,看代码注释就好了。 # -*- coding: utf-8 -*- # @Date : 2018-05-21 12:07:17 # @Author : Light (halysl0817@gmail.com) # @Link : ${link} # @Version : $Id$ import os import random """ TODO(halysl0817@gmail.com):Change the wallpaper by changing the time 利用linux下的crontab工具实现半小时切换一次, */30 * * * * python /xxx/changebackground.py """ # 更换壁纸指令,此为deepin更换指令,不代表所有linux发行版更换指令 cmd = "gsettings set com.deepin.wrap.gnome.desktop.background picture-uri " # 图片目录,用户可自主更换 path = "/home/light/Documents/code/spider-on-lol/lolSpider/lolSpider/img/hero_skin_img/full/" # 利用os.listdir()方法获取图片目录下的所有文件名的列表 pic_list = os.listdir(path) # 确切的文件位置,以及确切的更换指令 real_path = path + str(random.choices(pic_list))[2:-2] real_cmd = cmd + "\"" + real_path +"\"" # 执行 os.system(real_cmd) 4、自动切换壁纸 利用 Linux 下的 crontab 完成定时任务。 ...

HTTP 协议

[[Internet]] [[HTTP]] HTTP 协议 HTTP 简介 HTTP 协议(HyperText Transfer Protocol,超文本传输协议)是因特网上应用最为广泛的一种网络传输协议,基于 TCP/IP 通信协议来传递数据(HTML 文件, 图片文件, 查询结果等)。 HTTP 协议工作于客户端-服务端架构(C/S)上。浏览器作为 HTTP 客户端通过 URL 向 HTTP 服务端即 WEB 服务器发送所有请求。 Web 服务器有:Apache 服务器,IIS 服务器(Internet Information Services)等。 HTTP 默认端口号为 80,但是你也可以改为 8080 (代理服务器)或者其他端口。 HTTP 消息结构 客户端请求消息(请求头) 客户端发送一个 HTTP 请求到服务器的请求消息包括以下格式:请求行(request line)、请求头部(header)、空行和请求数据四个部分组成,如下图: 1、请求行 请求行由请求方法字段、URL 字段和 HTTP 协议版本字段 3 个字段组成,它们用空格分隔。比如: GET /data/info.html HTTP/1.1 方法字段就是 HTTP 使用的请求方法,比如常见的 GET/POST/HEAD。HTTP1.1 新增了五种请求方法:OPTIONS, PUT, DELETE, TRACE 和 CONNECT 方法。 URL 统一资源定位符 HTTP 协议版本有两种:HTTP1.0/HTTP1.1。 2、请求头部 大多数请求头并不是必需的,但 Content-Length 除外。对于 POST (向服务器提交数据)请求来说 Content-Length 必须出现。 ...

MySQL 内置函数

database [[MySQL]] MySQL 内置函数 字符串函数 查看字符的 ascii 码值 ascii(str),str 是空串时返回 0 select ascii('a'); 查看 ascii 码值对应的字符 char(数字) select char(97); 拼接字符串 concat(str1,str2…) select concat(12,34,'ab'); 包含字符个数 length(str) select length('abc'); 截取字符串 -- left(str,len) 返回字符串 str 的左端 len 个字符 -- right(str,len) 返回字符串 str 的右端 len 个字符 -- substring(str,pos,len) 返回字符串 str 的位置 pos 起 len 个字符 select substring('abc123',2,3); 去除空格 -- ltrim(str) 返回删除了左空格的字符串 str -- rtrim(str) 返回删除了右空格的字符串 str -- trim([方向 remstr from str) 返回从某侧删除 remstr 后的字符串 str,方向词包括 both、leading、trailing,表示两侧、左、右 select trim(' bar '); select trim(leading 'x' FROM 'xxxbarxxx'); select trim(both 'x' FROM 'xxxbarxxx'); select trim(trailing 'x' FROM 'xxxbarxxx'); 返回由 n 个空格字符组成的一个字符串 space(n) select space(10); 替换字符串 replace(str,from_str,to_str) select replace('abc123','123','def'); 大小写转换,函数如下 -- lower(str) -- upper(str) select lower('aBcD'); 数字函数 求绝对值 abs(n) select abs(-32); 求 m 除以 n 的余数 mod(m, n),同运算符 % select mod(10,3); select 10%3; 地板除 floor(n),表示不大于 n 的最大整数 select floor(2.3); 天花板 ceiling(n),表示不小于 n 的最大整数 select ceiling(2.3); 求四舍五入值 round(n,d),n 表示原数,d 表示小数位置,默认为 0 select round(1.6); 求 x 的 y 次幂 pow(x,y) select pow(2,3); 获取圆周率 PI() select PI(); 随机数 rand(),值为 0-1.0 的浮点数 select rand(); 时间日期函数 获取子值,语法如下 -- year(date)返回date的年份(范围在1000到9999) -- month(date)返回date中的月份数值 -- day(date)返回date中的日期数值 -- hour(time)返回time的小时数(范围是0到23) -- minute(time)返回time的分钟数(范围是0到59) -- second(time)返回time的秒数(范围是0到59) select year('2016-12-21'); 日期计算,使用 +- 运算符,数字后面的关键字为 year、month、day、hour、minute、second select '2016-12-21'+interval 1 day; 日期格式化 date_format(date,format),format 参数可用的值如下 -- 获取年%Y,返回4位的整数 -- 获取年%y,返回2位的整数 -- 获取月%m,值为1-12的整数 -- 获取日%d,返回整数 -- 获取时%H,值为0-23的整数 -- 获取时%h,值为1-12的整数 -- 获取分%i,值为0-59的整数 -- 获取秒%s,值为0-59的整数 select date_format('2016-12-21','%Y %m %d'); 当前日期 current_date() select current_date(); 当前时间 current_time() select current_time(); 当前日期时间 now() select now();

MySQL 数据库简介

database MySQL MySQL 数据库简介 mysql 数据库,是当前应用非常广泛的一款关系型数据库。 知识点包括: 数据库与表的创建、删除 字段的类型、约束 关系的存储 数据行的增删改查 数据行的查找(重点) 视图、事务、索引 与 Python 交互 E-R 模型 当前物理的数据库都是按照 E-R 模型进行设计的 E表示entry,实体 R表示relationship,关系 一个实体转换成数据库中的一个表 关系描述两个实体之间的对应规则,包括: 1.一对一 2.一对多 3.多对多 关系转换为数据库表中的一个列*在关系型数据库中一行就是一个对象 三范式 经过研究和对使用中问题的总结,对于设计数据库提出了一些规范,这些规范被称为范式 第一范式(1NF):列不可拆分 第二范式(2NF):唯一标识 第三范式(3NF):引用主键 后一个范式建立在前一个范式的基础上 数据完整性 一个数据库就是一个完整的业务单元,可以包含多张表,数据被存储在表中 在表中为了更加准确的存储数据,保证数据的正确有效,可以在创建表的时候,为表添加一些强制性的验证,包括数据字段的类型、约束 字段类型 数字:int,decimal 字符串:char,varchar,text 日期:datetime 布尔:bit 约束 主键:primary key 非空:not null 唯一:unique 默认:default 外键:foreign key 自动增长:auto_increment

MySQL 相关命令

database MySQL MySQL 相关命令 数据库连接 mysql -u username -p password:***** mysql> 数据库操作 创建数据库: create database dbname charsetutf-8; 删除数据库: drop database dbname; 切换数据库: use dbname; 查看当前选择的数据库: select database(); 展示所有数据库: show databases; 表操作 查看所有表: show tables; 创建表: create table tbname( 列及类型约束 ); # 例如: create table students( id int auto_increment primary key, sname varcahr(10) not null ); 修改表: alter table tbname add|change|drop 列名 类型; # 例如: alter table students add birthday datetime; 删除表: drop table tbname; 查看表结构: desc tbname; 更改表名称: rename table old-tbname to new-tbname; 查看表的创建语句: show create table tbname; 数据操作 查询: select * from tbname; 增加: ...

MySQL 连接 Python

database MySQL Python MySQL 连接 Python 0.安装引入模块 使用接口Python DB API 安装mysql模块 pip install mysql-connector-python # python2.7,使用 Python-MySQL connector pip install pymysql # python3.4,安装 pymysql 模块 引入模块 import pymsql #python3.x import mysql.connector #py2.x 1.connection 对象 用于建立与数据库的连接 创建方法: # host:连接的 mysql 主机,本机填为 ‘localhost’ # port:连接的 mysql 主机的端口,默认为 3306 # user:连接的用户名 # passwd:连接的用户的密码 # db:连接的数据库名 conn = pymysql.Connect(host,port,user,passwd,db) # Python 3.x connection 对象的方法 close():关闭连接 commit():提交修改 rollback():回滚事务 cursor():返回 cursor 对象 2.cursor对象 用于执行语句 创建方法: cursor = conn.cursor() 方法 execute(op,[args]) # 执行一个数据库查询和命令 fetchone() # 取得结果集的下一行 fetchmany(size) # 取得结果集的下几行 fetchall() # 取得结果集的剩下的所有行 rowcount # 最近一次execute返回数据的行数或影响行数 close() # 关闭游标对象 属性 rowcount 只读属性,表示最近一次 execute() 执行后受影响的行数 connection 获得当前连接对象 3.查询的一般流程 select查询数据操作过程: 开始 创建connection 创建cursor 使用cursor.excute()执行select语句 使用cursor.fetch*()获取并处理数据 关闭cursor 关闭connection 结束 4.常见查询语句 # 增加 import pymysql try: conn = pymysql.Connect(host,port,user,passwd,db) cs1=conn.cursor() count=cs1.execute("insert into students(sname) values('张良')") print(count) conn.commit() cs1.close() conn.close() except Exception,e: print e.message # 删除 # -*- coding: utf-8 -*- import pymysql try: conn = pymysql.Connect(host,port,user,passwd,db) cs1=conn.cursor() count=cs1.execute("delete from students where id=6") print(count) conn.commit() cs1.close() conn.close() except Exception,e: print e.message # 查询一行数据 #-*- coding: utf-8 -*- import pymysql try: conn = pymysql.Connect(host,port,user,passwd,db) cur=conn.cursor() cur.execute('select * from students where id=7') result=cur.fetchone() print() cur.close() conn.close() except Exception,e: print e.message # 查询多行数据 #-*- coding: utf-8 -*- import pymysql try: conn = pymysql.Connect(host,port,user,passwd,db) cur=conn.cursor() cur.execute('select * from students') result=cur.fetchall() print(result) cur.close() conn.close() except Exception,e: print e.message 5.SQL 语句参数化 以参数传递的方式执行 sql 脚本,为之后的封装打基础 例如: #-*- coding: utf-8 -*- import pymysql try: conn = pymysql.Connect(host,port,user,passwd,db) cs1=conn.cursor() sname=raw_input("请输入学生姓名:") #参数 params=[sname] #参数封装成列表 count=cs1.execute('insert into students(sname) values(%s)',params) #%传参 print(count) conn.commit() cs1.close() conn.close() except Exception,e: print e.message 6.封装 将常见的语句封装成方法,以传参的方式执行语句,防注入 例如: #-*- coding: utf-8 -*- import pymysql class MysqlHelper(): def __init__(self,host,port,db,user,passwd,charset='utf8'): self.host=host self.port=port self.db=db self.user=user self.passwd=passwd self.charset=charset def connect(self): self.conn=MySQLdb.connect(host=self.host,port=self.port,db=self.db,user=self.user,passwd=self.passwd,charset=self.charset) self.cursor=self.conn.cursor() def close(self): self.cursor.close() self.conn.close() def get_one(self,sql,params=()): result=None try: self.connect() self.cursor.execute(sql, params) result = self.cursor.fetchone() self.close() except Exception, e: print e.message return result def get_all(self,sql,params=()): list=() try: self.connect() self.cursor.execute(sql,params) list=self.cursor.fetchall() self.close() except Exception,e: print e.message return list def insert(self,sql,params=()): return self.__edit(sql,params) def update(self, sql, params=()): return self.__edit(sql, params) def delete(self, sql, params=()): return self.__edit(sql, params) def __edit(self,sql,params): count=0 try: self.connect() count=self.cursor.execute(sql,params) self.conn.commit() self.close() except Exception,e: print e.message return count

Python 连接 MySQL

MySQL database Python Python 连接 MySQL 使用接口 Python DB API,两种情况: python2.7,使用 Python-MySQL connector,载入语句 import MySQLdb python3.4,安装 pymysql 模块,载入语句 import pymysql Connection对象: 创建方法:pymysql.Connect(host,port,user,passwd,db) 支持方法: 1、cursor(),使用该连接创建并返回游标 2、commit(),提交当前事务 3、rollback(),回滚当前事务 4、close(),关闭连接 cursor游标对象:用于执行查询和获取结果 支持方法: 1、execute(op,[args]),执行一个数据库查询和命令 2、fetchone(),取得结果集的下一行 3、fetchmany(size),取得结果集的下几行 4、fetchall(),取得结果集的剩下的所有行 5、rowcount,最近一次execute返回数据的行数或影响行数 6、close(),关闭游标对象 select查询数据操作过程 开始 创建connection 创建cursor 使用cursor.excute()执行select语句 使用cursor.fetch*()获取并处理数据 关闭cursor 关闭connection -结束 insert/update/delete更新数据库操作过程 开始 创建connection 创建cursor 使用cursor.excute()执行insert/update/delete语句 -出现异常:使用connection.rollback()回滚事务 未出现异常:使用connection.commit()提交事务 关闭cursor 关闭connection 结束

scrapy框架初使用及爬取LOL信息

Python [[Web Crawler]] scrapy 框架初使用及爬取 LOL 信息 零、真前言 这篇文章是由 2018 年 4 月 16 日首次发布于 csdn,后来由于 “逃离csdn” 的想法中,将之前发布的文章重新排版,发表在个站中。 一、前言 了解到爬虫技术大概有 18 个月了,这期间自己写过几个爬虫,也 fork 过几个流行的爬虫 repo,包括 bilibili-user、iquery、WechatSogou 等,但一直没系统的写过爬虫,上一次心血来潮(17 年 10 月),想要爬下关于英雄联盟的数据,主要想获得皮肤原画数据。 当时决定的目标网站是英雄联盟中文官网,数据准确且更新快。LOL 数据库,但苦于该网页全篇使用 js 载入数据,无法直接读取,就退而求其次,改变目标网址多玩-英雄联盟-英雄数据库。 技术使用混杂,主要利用 requests+bs4 完成爬取,部分内容使用 scrapy 抓取,逻辑结构混乱,代码可见 spider-on-lol/v1.0/,功能基本实现,但不够美。七天之前,看到了这些代码,决定用 scrapy 重新实现功能,完成整个逻辑设计,这中间遇到了很多问题,从中得到了学习,完整代码可见 spider-on-lol。 二、爬虫设计全思路 目标网站:LOL 数据库 爬取内容: 英雄姓名 英雄头像 物品名称、id 物品图片 英雄皮肤原画 英雄背景故事 技术:scrapy,splash,docker 难点 图片获取 js 页面数据的获取 中文编码 三、环境搭建 工欲善其事,必先利其器。 1、 开发语言:python v3.6.5 2、开发语言环境:anaconda v1.6.9 (非必须,但隔离环境是一个好习惯) 3、docker 安装 deepin 下安装 docker 其他系统安装 docker 4、splash 安装方法 5、一些第三方库: scrapy:conda install Scrapy scrapy_splash: conda install scrapy_splash 四、什么是 Scrapy?什么是 Splash? 1、Scrapy Scrapy 是一个为了爬取网站数据,提取结构性数据而编写的应用框架。 可以应用在包括数据挖掘,信息处理或存储历史数据等一系列的程序中。 ...

scrapy框架初使用及爬取LOL信息

Python Web Crawler Scrapy with bilibili 一、前言 在一个月前,我写了一篇scrapy 框架初使用及爬取 LOL 信息记录了爬取 lol.qq.com 获取英雄联盟数据及英雄皮肤原画的过程。第一次使用 scrapy 后,了解了大致的爬取流程,但在细节上(例如防 ban 策略,奇怪数据处理)没太在意,处于编码第一阶段(能跑就行)。 中间学了半个月的 Qt5 和 pygame,(没学出个什么样子,了解了大致概念,翻指南能上手了),之后,看到 github 中早期 fork 了一个库,airingursb 写的bilibili-user,深有所悟,在此先感谢他的源码及他的开源精神。 但最近一段时间,BiliBili 的网站结构有了些许的变化,我就尝试着用 scrapy 重写这个功能,以只修改 item 的方式保证这个爬虫的生命(理论上,更换 item 对应的 xpath 位置就可以应对页面元素更改)。并在此基础上增加一些防 ban 策略,深化对爬虫的编写能力,以及应对可能过大的数据处理任务(单纯的构造 url,截止 5 月 3 日,b 站已经有了 323000449 账号详情界面,之前的 lol 爬虫上千条数据就把路由器撑爆了,这次可能要应付 3 亿条数据)。完整代码可见bilibili-user-scrapy 二、爬虫设计全思路 1、目标网站 账户详情页 2、爬取内容: uid 用户id,int mid 用户id,str name 用户姓名,str sex 用户性别,str regtime 用户注册时间,str birthday 用户生日,str place 用户住址,str fans 用户粉丝数,int attention 用户关注数,int level 用户等级,int 3、技术:scrapy,splash,docker,mysql 4、难点 数据库设计及数据插入 js 页面数据的获取 特殊数据的处理 防 ban 策略 三、环境搭建 1、开发语言:python v3.6.5 2、开发语言环境:anaconda v1.6.9 (非必须,但这是一个好习惯) 3、docker 安装 deepin 下安装 docker 其他系统安装 docker ...