2021年2月

如果是直接使用pip install M2Crypto安装的话就会出现

SWIG/_m2crypto.i:62: Error: Unable to find 'openssl/opensslv.h'
  SWIG/_m2crypto.i:68: Error: Unable to find 'openssl/safestack.h'
  SWIG/_evp.i:12: Error: Unable to find 'openssl/opensslconf.h'
  SWIG/_rc4.i:5: Error: Unable to find 'openssl/opensslconf.h'
  SWIG/_ec.i:7: Error: Unable to find 'openssl/opensslconf.h'
  error: command 'swig' failed with exit status 1
  ----------------------------------------
  ERROR: Failed building wheel for m2crypto

这样的错误(没写完整哈),这是由于M2Crypto有两个依赖,openssl和swig,在Mac上需要首先安装这两个包,才能安装M2Crypto。
我们可以通过brew来安装这两个包,如果你的电脑没有brew的话,你就得先安装brew,小编在[post cid="773" /]这篇文章中介绍过怎么安装。下面我们来安装openssl和swig。

brew install openssl
brew install swig
# 有时候需要手动链接swig
# brew unlink swig && brew link swig

这时候就可以安装M2Crypto,不过还是有时候会报错,我们需要指定下地址

env LDFLAGS="-L$(brew --prefix openssl)/lib" CFLAGS="-I$(brew --prefix openssl)/include" SWIG_FEATURES="-cpperraswarn -includeall -I$(brew --prefix openssl)/include"

然后我们就可以安装了M2Crypto

pip install M2Crypto

macOS升级至macOS Big Sur 后,我们使用brew安装或者更新软件的时候会出现Error: Your CLT does not support macOS 11.2.的错误,我们只需要重新安装下xcode-select就可以解决了。
解决办法:
输入以下命令

sudo rm -rf /Library/Developer/CommandLineTools
sudo xcode-select --install 

这时候就会提示安装 xcode-select,在弹出框里我们点击安装,然后就弹出协议,我们再点击同意。然后就等待安装完毕就可以了。

小编发现最近在使用git diff这个命令的时候,有些代码明明没有修改,却标识修改过,末尾加上了个^M。很奇怪啊,我们先说下^M的由来:
这个是由于 Windows 和 Unix 下的换行符不一致导致的,Windows 下,换行符是 \r\n,在 Unix 下换行符是 \n。如果我们用把一个文件的换行符换成 Windows 的换行符就会出现这样的问题,实际上 ^M 就是 Windows 下的换行符中的 \r 部分。因为 Unix 下的换行符是 \n,所以当一个用 Windows 下的换行符的文件放在 Unix 下的时候,单行的最后一个字符就变成了 \r\r 在 ASCII 码中是 0xD,而 0xD 在 VIM 和 cat -v 则刚好被显示为 ^M,这次明白了吧。

解决办法:
我们可以git diff的时候忽略换行符的差异:

git config --global core.whitespace cr-at-eol

小编今天在利用Arduino开发工具开发esp8266的程序的时候,发现无法写入,出现这样的错误

问题:
Executable segment sizes:
IROM   : 243180          - code in flash         (default or ICACHE_FLASH_ATTR)
IRAM   : 26888   / 32768 - code in IRAM          (ICACHE_RAM_ATTR, ISRs...)
DATA   : 1264  )         - initialized variables (global, static) in RAM/HEAP
RODATA : 964   ) / 81920 - constants             (global, static) in RAM/HEAP
BSS    : 24968 )         - zeroed variables      (global, static) in RAM/HEAP
项目使用了 272296 字节,占用了 (26%) 程序存储空间。最大为 1044464 字节。
全局变量使用了27196字节,(33%)的动态内存,余留54724字节局部变量。最大为81920字节。
pyserial or esptool directories not found next to this upload.py tool.

解决办法:
打开 arduino esp8266 sdk 目录: ~/Library/Arduino15/packages/esp8266/hardware/esp8266/2.7.4/tools/
打开浏览器下载最新版 esptool 和 pyserial
下载地址:

https://github.com/espressif/esptool/archive/v3.0.zip
https://github.com/pyserial/pyserial/archive/v3.4.zip

解压后,把文件夹重命名为esptool和pyserial,然后直接替换就可以了。

飞鹅标签机是支持打印图片的,不过只能识别黑色,灰色居然给忽略了,毕竟是热敏打印机,只能打印黑色,不知道是不是刚出来的原因,无论是官方的文档,还是提供的DEMO,都不是很完善,特别是打印图片的,只提供了PHP的DEMO。可惜小编是用Python写的,下面小编分享下,Python的案例:
注意:官方这么要求图片的:图片二进制数据,需配合标签使用,最佳效果为不大于224px的正方形(宽高都为8的倍数)黑白图,支持jpg、png、bmp,不能超过10K。 小编提醒大家:图片一定要用正方形的。

#coding:utf-8

import time
import hashlib
import requests
import io
from requests_toolbelt import MultipartEncoder

def printLabelMsg(qr_addredd, serial):
    # 初始化标签大小
    # content = '<SIZE>60,40</SIZE>'
    content = ""
    content += '<IMG x="200" y="30">'
    content += '<TEXT x="280" y="55" font="12" w="2" h="2" r="0">打印图片</TEXT>'
    STIME = str(int(time.time()))#不需要修改
    LOGO = "本地图片路径"
    params = {
        'user':******,
        'stime':*******,
        'sig':signature(STIME),
        'apiname':'Open_printLabelMsg',
        'sn':******,
        'content':content,
        'times': TIMES,
        'img': (LOGO, open(LOGO, 'rb'), 'application/jpeg')
    }
    m = MultipartEncoder(params)
    response = requests.post('http://api.feieyun.cn/Api/Open/', data=m,headers={'Content-Type': m.content_type}, timeout=30)
    code = response.status_code #响应状态码
    if code==200:
        con_str=str(response.content, encoding = "utf-8")  
        content=eval(con_str)
        if(content.get('ret')!=0):
            raise RuntimeError(con_str)
        print(response.content)#服务器返回的JSON字符串,建议要当做日志记录起来
    else:
        raise RuntimeError('code{}'.format(code))

下面小编提供下官方客服给的PHP的案例:

<?php


function printLabelMsg($user, $skey, $sn, $content, $times = 1)
{
    $url = 'http://api.feieyun.cn/Api/Open/';
    $stime = time();
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, [
        'user' => $user,
        'stime' => $stime,
        'sig' => sha1($user.$skey.$stime),
        'apiname' => 'Open_printLabelMsg',
        'sn' => $sn,
        'content' => $content,//这里就是配合使用的IMG标签
        'times' => $times,
        'img' => new \CURLFile('test.jpg')
    ]);
    $headers = array('Content-Type: multipart/form-data;');//传图
     curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
    $response = curl_exec($ch);
    curl_close($ch);

    var_dump( $response );
}

printLabelMsg('*************', '*******************', '**********', '<img x="50" y="10">');//USER,UKEY,打印机编号

做过爬虫的小伙伴应该都很清楚要伪装自己的请求,包括请求头等,同时也要隐藏自己的ip,不然的话很快自己的ip就被封了,访问不了了。免费的代理ip很多,也有收费的,要是你的资金允许,或者对ip要求比较高的话,建议直接去购买收费的代理ip,直接通过接口就能获取。小编在这里主要是说下爬取的快代理的免费ip的部分,并且验证他对自己要访问的网站是否可以访问,并且保存到redis或者文件当中。
我们打开快代理的网站,免费代理的页面https://www.kuaidaili.com/free/inha/1/
好了,下面下面分享下爬取的代码吧

import requests
from lxml import etree
from fake_useragent import UserAgent
import redis
import threading
import time


pool = redis.ConnectionPool(host='127.0.0.1', port=6379, db=1, password='')

# 测试的URL
test_url = "https://www.1688.com/"

def get_proxy(url):
    '''爬取快代理的免费代理ip'''
    headers= {'User-Agent':str(UserAgent(verify_ssl=False).random)}
    response = requests.get(url,headers=headers)
    selector = etree.HTML(response.text)
    proxies = []
    for each in selector.xpath('//table[@class="table table-bordered table-striped"]/tbody/tr')[1:]:
        ip = each.xpath("./td[1]/text()")[0]
        port = each.xpath("./td[2]/text()")[0]
        proxy = ip + ":" + port
        proxies.append(proxy)
    test_proxies(proxies, test_url, 'ip_proxy')

def test_proxies(proxies, test_url , ipname):
    '''检查IP是否可以使用'''
    proxies = proxies
    normal_proxies = []
    count = 1
    for proxy in proxies:
        print("第{}个, {}".format(count, proxy))
        count += 1
        try:
            headers= {'User-Agent':str(UserAgent(verify_ssl=False).random)}
            response = requests.get(test_url, headers=headers, proxies={"http": proxy}, timeout=1)
            time = response.elapsed.total_seconds()
            if response.status_code == 200:
                print("{}可以使用".format(proxy))
                normal_proxies.append(proxy)
            else:
                print("{}--{}--可以不可以访问".format(proxy, response.status_code))
        except Exception as e:
            print("{}无效".format(proxy))
    write_proxy(normal_proxies, ipname)

def write_proxy(proxies, name):
    '''写入文件或redis'''
    for proxy in proxies:
        # 写入redis
        r = redis.Redis(connection_pool=pool)
        r.lpush(name, proxy)
        print(r.lrange(name, 0, -1))
        # 写入文件
        with open("./{}.txt".format(name), 'a+') as f:
            f.write(proxy + '\n')
    print("录入完成!!!")

if __name__ == "__main__":
    base_url = "https://www.kuaidaili.com/free/inha/{}/"
    for i in range(1, 3000):
        print("第{}页".format(i))
        url = base_url.format(i)
        get_proxy(url)

5.jpg