shuaiqi 发布的文章

Centos7安装python3+Selenium+chrome+chromedriver详细教程


Centos7安装python3+Selenium+chrome+chromedriver详细教程
python2和python3共存,Selenium错误的处理
更新Centos源

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
或者
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
#下载完后,运行下面命令:
yum clean all
yum makecache

1.Python3安装与python2共存

wget http://mirrors.sohu.com/python/3.6.2/Python-3.6.2.tar.xz
yum install libffi-devel expat-devel gdbm-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make
mv /usr/bin/python /usr/bin/python.bak
tar -xvJf  Python-3.6.2.tar.xz
cd Python-3.6.2
./configure prefix=/usr/local/python3
make && make install
make clean
ln -s /usr/local/python3/bin/python3 /usr/bin/python

python -V 检查下是不是python3
python2 -V 检查下是不是python2
如果上面正常显示,请继续设置下。yum需要python2版本,所以我们还要修改yum的配置。/usr/libexec/urlgrabber-ext-down也需要修改python2

vi /usr/bin/yum
#把文件第一行python改成python2
#!/usr/bin/python2
......继续修改urlgrabber-ext-down
vi /usr/libexec/urlgrabber-ext-down
#跟上面一样修改第一行python改成python2

python2和python3共存:默认pip是python2,python3需要如何配置?如果pip也没有安装,就先安装pip

yum -y install epel-release
yum install python-pip
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3

配置pip源按自己需要,也可以不配置

mkdir ~/.pip
vi pip.conf

[global]
timeout = 60
index-url = https://pypi.doubanio.com/simple

2.chrome安装和chromedriver下载
chrome下载安装

yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm

chromedriver下载,我下载的是最新版本。chrome也是最新版本

https://npm.taobao.org/mirrors/chromedriver/2.40/chromedriver_linux64.zip

3.安装selenium,使用是的python3

pip3 install selenium

test.py测试代码如下:chrome界面浏览

# -*- coding:utf-8 -*-

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
options.add_argument('--no-sandbox')
driver = webdriver.Chrome(executable_path="/root/test/chromedriver", chrome_options=options)
driver.get("https://www.baidu.com")
print(driver.page_source)
driver.quit()

js 判断当前操作系统是ios还是android还是电脑端


js判断客户端是否是IOS或者是Android

//如果返回true 则说明是Android
function is_weixin() {
     var ua = window.navigator.userAgent.toLowerCase();
     if (ua.match(/MicroMessenger/i) == 'micromessenger') {
         return true;
     } else {
         return false;
     }
}

js判断手机操作系统(ios或者是Android)

//如果返回的是false说明当前操作系统是手机端,如果返回的是true则说明当前的操作系统是电脑端
function IsPC() {
var userAgentInfo = navigator.userAgent;
var Agents = ["Android", "iPhone",
    "SymbianOS", "Windows Phone",
    "iPad", "iPod"];
var flag = true;
for (var v = 0; v < Agents.length; v++) {
    if (userAgentInfo.indexOf(Agents[v]) > 0) {
        flag = false;
        break;
    }
}
return flag;
}

mysql与sqlite的数据迁移


一、
1.把sqlite数据库文件和sqlite-to-mysq.py文件放到linux系统下同一文件夹。linux系统得是安装了python2.7版本的
2.找到mysql的my.ini文件,修改为max_allowed_packet=50M,添加wait_timeout=288000 interactive_timeout = 288000 两个配置。max_allowed_packet是mysql允许最大的数据包,也就是你发送的请求; wait_timeout是等待的最长时间,这个值大家可以自定义,但如果时间太短的话,超时后就会现了MySQL server has gone away #2006错误。max_allowed_packet参数的作用是,用来控制其通信缓冲区的最大长度。
如果是linux系统。则找到my.cnf 添加那三条命令
3.重启你的mysql服务
4.执行sqlite3 sqlite文件名 .dump | ./sqlite3-to-mysql.py -u mysql数据库账号 -p mysql数据库密码 -d 迁移到mysql后的数据库名字 | mysql -u root -p --default-character-set=utf8 命令
二、
在数据库管理软件里,选择sqlite中你要迁移的数据库表,右键选择转储SQL文件->数据和结构。然后打开,把里面的"表名"全部替换成表名

sqlite3-to-mysql.py代码:

#! /usr/bin/env python
import re, fileinput, tempfile
from optparse import OptionParser


IGNOREDPREFIXES = [
    'PRAGMA',
    'BEGIN TRANSACTION;',
    'COMMIT;',
    'DELETE FROM sqlite_sequence;',
    'INSERT INTO "sqlite_sequence"',
]

REPLACEMAP = {"INTEGER PRIMARY KEY": "INTEGER AUTO_INCREMENT PRIMARY KEY",
    "AUTOINCREMENT": "AUTO_INCREMENT",
    "DEFAULT 't'": "DEFAULT '1'",
    "DEFAULT 'f'": "DEFAULT '0'",
    ",'t'": ",'1'",
    ",'f'": ",'0'",
}

def _replace_match_allcase(line, src, dst):
    line = line.replace(src,dst)
    line = line.replace(src.lower(),dst)
    return line

def _replace(line):
    if any(line.startswith(prefix) for prefix in IGNOREDPREFIXES):
        return
    for (src,dst) in REPLACEMAP.items():
        line = _replace_match_allcase(line, src, dst)
    return line

def _backticks(line, in_string):
    """Replace double quotes by backticks outside (multiline) strings

    >>> _backticks('''INSERT INTO "table" VALUES ('"string"');''', False)
    ('INSERT INTO `table` VALUES (\\'"string"\\');', False)

    >>> _backticks('''INSERT INTO "table" VALUES ('"Heading''', False)
    ('INSERT INTO `table` VALUES (\\'"Heading', True)

    >>> _backticks('''* "text":http://link.com''', True)
    ('* "text":http://link.com', True)

    >>> _backticks(" ');", True)
    (" ');", False)

    """
    new = ''
    for c in line:
        if not in_string:
            if c == "'":
                in_string = True
            elif c == '"':
                new = new + '`'
                continue
        elif c == "'":
            in_string = False
        new = new + c
    return new, in_string

def _process(opts, lines):
    if opts.database:
        yield '''\
drop database IF EXISTS {d};
create database {d} character set utf8;
grant all on {d}.* to {u}@'localhost' identified by '{p}';
use {d};\n'''.format(d=opts.database, u=opts.username, p=opts.password)
    yield "SET sql_mode='NO_BACKSLASH_ESCAPES';\n"

    in_string = False
    for line in lines:
        if not in_string:
            line = _replace(line)
            if line is None:
                continue
        line, in_string = _backticks(line, in_string)
        yield line

def _removeNewline(line, in_string):
    new = ''
    for c in line:
        if not in_string:
            if c == "'":
                in_string = True
        elif c == "'":
            in_string = False
        elif in_string:
            if c == "\n":
                 new = new + 'Newline333'
                 continue
            if c == "\r":
                 new = new + 'carriagereturn333'
                 continue
        new = new + c
    return new, in_string
    
def _replaceNewline(lines):
    for line in lines:
           line = line.replace("Newline333", "\n")
           line = line.replace("carriagereturn333", "\r")
           yield line

def _Newline(lines):
    in_string = False
    for line in lines:
        if line is None:
           continue
        line, in_string = _removeNewline(line, in_string)
        yield line
    
def main():
    op = OptionParser()
    op.add_option('-d', '--database')
    op.add_option('-u', '--username')
    op.add_option('-p', '--password')
    opts, args = op.parse_args()
    lines = (l for l in fileinput.input(args))
    lines = (l for l in _Newline(lines))
    f = tempfile.TemporaryFile()
    for line in lines:
        f.write(line)
    f.seek(0)
    lines = (l for l in f.readlines())
    f.close()
    lines = (l for l in _process(opts, lines))
    for line in _replaceNewline(lines):
       print line,

if __name__ == "__main__":
    main()

win7使用php远程ssh连接linux系统进行数据传输及其他操作


1.win7上安装php
2.win7上安装ssh插件
下载 php extension ssh2
下载地址 http://windows.php.net/downloads/pecl/releases/ssh2/0.12/
根据自己PHP的版本去下载,
3.下载解压完后,会有三个文件,libssh2.dll、php_ssh.dll、php_ssh2.pdb。
4.3. 将 php_ssh.dll、php_ssh2.pdb 放到你的 php对应版本 扩展目录下 php/ext/ 下。
5.4. 将libssh2.dll 复制到 c:/windows/system32 和c:/windows/syswow64 各一份
6.在php.ini配置文件中加入一行 extension=php_ssh2.dll
7.6重启php服务器,即可使用php执行ssh连接操作了。
查看phpinfo(),看是否有显示ssh2扩展,如显示,则成功

//远程连接ssh端
$connection = ssh2_connect('192.168.2.80', 22);
ssh2_auth_password($connection, 'root', 'root123');
//远程执行ssh端命令
$stdout_stream = ssh2_exec($connection, 'touch /home/test/test.txt');
//从本地复制文件到ssh端
//ssh2_scp_send($connection, 'd:test1.txt', '/home/test/test1.txt', 0644);默认权限为0644,返回值为bool值,true或false.
//从ssh端复制文件到本地
ssh2_scp_recv($connection, '/home/test/test.txt', 'd:test.txt');

首先PHP有两种版本。一种是PHP-ts,一种是PHP-nts
ts(Thread-Safety)即线程安全,多线程访问时,采用了加锁机制,当一个线程访问该类的某个数据时,进行保护,其他线程不能进行访问直到该线程读取完,其他线程才可使用。不会出现数据不一致或者数据污染php以ISAPI方式加载的时候选择这个版本.,php以ISAPI方式加载的时候选择这个版本.

nts(None-Thread Safe)即非线程安全,就是不提供数据访问保护,有可能出现多个线程先后更改数据造成所得到的是脏数据。php以fast cgi方式运行的时候选择这个版本,具有更好的性能;

ISAPI(Internet Server Application Programming Interface), 通常是指被http服务器所加载,以服务器的模块形式运行,由微 软提出,故只能在win平台上运行,例如win下的apache,iis[据说用fast cgi 方式工作更稳定],而linux上php则以 Apache模块或者php-fpm的方式运行。

cgi(Common Gateway Interface):HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,说白了,cig就是一种后台语言,其可以和服务器之间进行通讯。此时的php是作为一个独立的程序运行的。特点就是耗费内存。

fast cgi.是一个常驻(long-live)型的CGI,它可以一直执行着,只要激活后,不会每次都要花费时间去fork.这种方式是与语言无关的、可伸缩架构的CGI开放扩展,其主要行为是将CGI解释器进程保持在内存中并因此获得较高的性能。

如何选择:
通常win下php和apache组合,以isapi的方式运行,而linux下可以是apche+php,php常常作为apache的模块,而nginx+php时,phpfast cgi的方式,即php-fpm的方式跑。

昨天安装ssh2扩展时我选择的php版本是nts的。结果发现运行数据分析时,总是弄到一半就出错。然后我改为ts版本,就能运行。

所以安装ssh2扩展时,如果你以后服务器上还得运行类似数据分析这种多线程的程序,就选择ts版本的ssh2扩展。