分类 【编程开发】 下的文章

解决Python 3.6使用pip时Unable to locate finder for pip._vendor.distlib的问题

最近在笔记本上安装了Python 3.6 x86-64位版本,习惯性的创建了一个virtualenv,然后使用pip安装第三库的时候发生以下错误:

Collecting virtualenv
  Downloading virtualenv-15.0.3-py2.py3-none-any.whl (3.5MB)
    100% |████████████████████████████████| 3.5MB 256kB/s
Installing collected packages: virtualenv
Exception:
Traceback (most recent call last):
  File "c:\users\corey shaw\appdata\local\programs\python\python36\lib\site-packages\pip\basecommand.py", line 215, in main
    status = self.run(options, args)
  File "c:\users\corey shaw\appdata\local\programs\python\python36\lib\site-packages\pip\commands\install.py", line 317, in run
    prefix=options.prefix_path,
  File "c:\users\corey shaw\appdata\local\programs\python\python36\lib\site-packages\pip\req\req_set.py", line 742, in install
    **kwargs
  File "c:\users\corey shaw\appdata\local\programs\python\python36\lib\site-packages\pip\req\req_install.py", line 831, in install
    self.move_wheel_files(self.source_dir, root=root, prefix=prefix)
  File "c:\users\corey shaw\appdata\local\programs\python\python36\lib\site-packages\pip\req\req_install.py", line 1032, in move_wheel_files
    isolated=self.isolated,
  File "c:\users\corey shaw\appdata\local\programs\python\python36\lib\site-packages\pip\wheel.py", line 493, in move_wheel_files
    maker.make_multiple(['%s = %s' % kv for kv in console.items()])
  File "c:\users\corey shaw\appdata\local\programs\python\python36\lib\site-packages\pip\_vendor\distlib\scripts.py", line 383, in make_multiple
    filenames.extend(self.make(specification, options))
  File "c:\users\corey shaw\appdata\local\programs\python\python36\lib\site-packages\pip\_vendor\distlib\scripts.py", line 372, in make
    self._make_script(entry, filenames, options=options)
  File "c:\users\corey shaw\appdata\local\programs\python\python36\lib\site-packages\pip\_vendor\distlib\scripts.py", line 276, in _make_script
    self._write_script(scriptnames, shebang, script, filenames, ext)
  File "c:\users\corey shaw\appdata\local\programs\python\python36\lib\site-packages\pip\_vendor\distlib\scripts.py", line 212, in _write_script
    launcher = self._get_launcher('t')
  File "c:\users\corey shaw\appdata\local\programs\python\python36\lib\site-packages\pip\_vendor\distlib\scripts.py", line 351, in _get_launcher
    result = finder(distlib_package).find(name).bytes
  File "c:\users\corey shaw\appdata\local\programs\python\python36\lib\site-packages\pip\_vendor\distlib\resources.py", line 324, in finder
    raise DistlibException('Unable to locate finder for %r' % package)
pip._vendor.distlib.DistlibException: Unable to locate finder for 'pip._vendor.distlib'

解决方案如下:
1.激活对应的virtualenv,然后执行:

python -m pip uninstall pip setuptools

2.下载

https://bootstrap.pypa.io/get-pip.py

3.同样在virtualenv激活状态下,执行python get-pip.py,这里要注意路径的问题,很可能路径没配置,导致了把原有python进行了pip卸载重装的动作

限制tomcat仅响应本机请求

在VPS上搭建了nginx和tomcat应用,想通过nginx来反向代理127.0.0.1:8080的tomcat对外提供服务,但是8080端口始终对外所有地址提供监听,修改方法有以下三种:
方案一:使用防火墙拦截8080端口
这个是第一个能想到的做法,直接在iptables里做就可以了,不做过多陈述;

方案二:修改tomcat全局仅监听127.0.0.1
在$CATALINA_HOME/conf/server.xml中找到类似以下描述8080监听的标签段:

<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

在该标签中添加:address="127.0.0.1"
完成后为:

<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               address="127.0.0.1"
               redirectPort="8443" />

这样tomcat下发布的所有应用都只能被127.0.0.1访问到了

方案三:修改tomcat下的某一应用仅监听127.0.0.1
在$CATALINA_HOME/conf/server.xml中找到应用路径配置的标签,比如:

<context path="/blog" reloadable="true" docBase="/var/www/blog" />

在context标签中添加监听绑定后修改为:

<context path="/blog" reloadable="true" docBase="/var/www/blog">
    <value className="org.apache.catalina.values.RemoteAddrValue" allow="127.0.0.1" deny="" />
</context>

也可以设置为被特定地址段访问,比如只允许被192.168.1.0-192.168.5.255和192.168.10.0-192.168.15.255这样的IP段访问:

<context path="/blog" reloadable="true" docBase="/var/www/blog">
    <value className="org.apache.catalina.values.RemoteAddrValue"
        allow="192.168.[1-5].*,192.168.[10-15].*" deny="" />
</context>

以上针对tomcat配置文件修改的方案,均为重启tomcat后生效

解决 golang在macos编译时fatal error: MSpanList_Insert错误

mac自从抹盘安装macOS Sierra (version 10.12)之后,就一直没有安装golang环境,这几天想写一个运维小工具,思来想去还是golang最合适,编译后的体积虽然大了一些,但是胜在完全不依赖任何环境、任何第三方库,于是开始了golang环境的折腾。
按照以往惯例,安装golang 1.5+以上版本的时候,如果需要解决交叉编译环境的问题时,需要安装golang1.4.x,于是轻车熟路的先brew安装了gvm,然后gvm install go1.4.3,再然后设置$GOROOT_BOOTSTRAP环境变量为go1.4.3的安装路径,再来gvm install go1.7.3,报错如下:

failed MSpanList_Insert 0x905000 0x9b51f3c97b8 0x0 0x0
fatal error: MSpanList_Insert
runtime stack:
runtime.throw(0x5e5cf0, 0x10)
    /home/travis/.gimme/versions/go1.6.linux.amd64/src/runtime/panic.go:530 +0x90 fp=0x7fff5fbff0b0 sp=0x7fff5fbff098
runtime.(*mSpanList).insert(0x865d08, 0x905000)
    /home/travis/.gimme/versions/go1.6.linux.amd64/src/runtime/mheap.go:933 +0x293 fp=0x7fff5fbff0e0 sp=0x7fff5fbff0b0
runtime.(*mheap).freeSpanLocked(0x865500, 0x905000, 0x100, 0x0)
    /home/travis/.gimme/versions/go1.6.linux.amd64/src/runtime/mheap.go:809 +0x4be fp=0x7fff5fbff148 sp=0x7fff5fbff0e0
runtime.(*mheap).grow(0x865500, 0x8, 0x0)
以下省略……

网上找了一坨坨的解决方案,都说重新安装golang1.7云云
最后,抱着试一试的态度,从golang官方下载了go1.7.3的pkg包,安装后,将$GOROOT_BOOTSTRAP指向了go1.7.3的安装目录(/usr/local/go),问题解决

golang执行远程ssh命令,以及scp文件

// main.go
package main

import (
    "fmt"
    "log"
    "os"
    "path"
    "time"

    "github.com/pkg/sftp"
    "golang.org/x/crypto/ssh"
)

func sshconnect(user, password, host string, port int) (*ssh.Session, error) {
    var (
        auth         []ssh.AuthMethod
        addr         string
        clientConfig *ssh.ClientConfig
        client       *ssh.Client
        session      *ssh.Session
        err          error
    )
    // get auth method
    auth = make([]ssh.AuthMethod, 0)
    auth = append(auth, ssh.Password(password))

    clientConfig = &ssh.ClientConfig{
        User:    user,
        Auth:    auth,
        Timeout: 30 * time.Second,
    }

    // connet to ssh
    addr = fmt.Sprintf("%s:%d", host, port)

    if client, err = ssh.Dial("tcp", addr, clientConfig); err != nil {
        return nil, err
    }

    // create session
    if session, err = client.NewSession(); err != nil {
        return nil, err
    }

    return session, nil
}

func sftpconnect(user, password, host string, port int) (*sftp.Client, error) {
    var (
        auth         []ssh.AuthMethod
        addr         string
        clientConfig *ssh.ClientConfig
        sshClient    *ssh.Client
        sftpClient   *sftp.Client
        err          error
    )
    // get auth method
    auth = make([]ssh.AuthMethod, 0)
    auth = append(auth, ssh.Password(password))

    clientConfig = &ssh.ClientConfig{
        User:    user,
        Auth:    auth,
        Timeout: 30 * time.Second,
    }

    // connet to ssh
    addr = fmt.Sprintf("%s:%d", host, port)

    if sshClient, err = ssh.Dial("tcp", addr, clientConfig); err != nil {
        return nil, err
    }

    // create sftp client
    if sftpClient, err = sftp.NewClient(sshClient); err != nil {
        return nil, err
    }

    return sftpClient, nil
}

func main() {

    session, err := sshconnect("用户名", "密码", "IP地址", 端口)
    if err != nil {
        log.Fatal(err)
    }
    defer session.Close()
    session.Stdout = os.Stdout
    session.Stderr = os.Stderr
    session.Run("ls")
    fmt.Println("upload file test:")

    var (
        sftpClient *sftp.Client
    )

    // 这里换成实际的 SSH 连接的 用户名,密码,主机名或IP,SSH端口
    sftpClient, err = sftpconnect("用户名", "密码", "IP地址", 端口)
    if err != nil {
        log.Fatal(err)
    }
    defer sftpClient.Close()

    // 用来测试的本地文件路径 和 远程机器上的文件夹
    var localFilePath = "main.go"
    var remoteDir = "/root/"
    srcFile, err := os.Open(localFilePath)
    if err != nil {
        log.Fatal(err)
    }
    defer srcFile.Close()

    var remoteFileName = path.Base(localFilePath)
    dstFile, err := sftpClient.Create(path.Join(remoteDir, remoteFileName))
    if err != nil {
        log.Fatal(err)
    }
    defer dstFile.Close()

    buf := make([]byte, 1024)
    for {
        n, _ := srcFile.Read(buf)
        if n == 0 {
            break
        }
        dstFile.Write(buf[0:n])
    }

    fmt.Println("copy file to remote server finished!")
}

Python某系统登录后自动提交

自己能看懂就行 ^_^

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
__author__ = 'tan9le'

import requests
import datetime
import random

host = '服务器地址+端口'
Origin = 'http://服务器地址+端口/'
indexURL = Origin + 'attp/www/index.html'
loginURL = Origin + 'attp/login/login.do'
operURL = Origin + 'attp/attendance/entryForJsp'
user_name = '用户名'
password = '密码'

today = datetime.date.today()
working_days = ['2016-09-17','2016-10-08','2016-10-09']
holidays = ['2016-10-03','2016-10-04','2016-10-04','2016-10-06','2016-10-07']

#通用header
header_base = {
    'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36',
    'Host': host,
    'Origin':Origin,
    'Connection':'keep-alive',
    'Referer':indexURL
}
#登录时引用的header
loginHeader = header_base.copy()
loginHeader["Content-Type"] = 'application/json;charset=UTF-8'
#操作时引用的header
operHeader = header_base.copy()

#全局request session,用来解决重复请求时的session、cookeis问题
s = requests.session()

def init():
    #得到日期对应的字符串
    todaystr = today.strftime("%Y-%m-%d")
    #得到日期是星期几,0代表周一
    dayofweek = today.weekday()
    #判断是否应该打卡
    if dayofweek < 6:
        if(todaystr in holidays):
            operflag = False
            print("工作日放假^_^")
        else:
            operflag = True
            print("正常上班,需要打卡")
    else:
        if(todaystr in working_days):
            operflag = True
            print("周末正常上班,需要打卡")
        else:
            operflag = False
            print("周末正常休息^_^")
    return operflag

#登录请求
def login():
    loginHeader['request-type'] = 'angular/api'
    login_param = {"login_id":user_name,"password":password,"roleType":"0"}
    #登录
    s.headers = loginHeader
    s.post(loginURL,json=login_param)
    #访问初始界面
    r = s.get(Origin + 'attp/attendance/initPage')
    #登录成功后,将header还原
    s.headers = header_base
    return r

#出勤
def chuqin(yearmonth,begin_time):
    operHeader['Referer'] = Origin + 'attp/attendance/initAttForJsp'
    oper_param = {"staff_code":user_name,"yearmonth":yearmonth,"project_id":"-1","projectname":" ","projectcode":"-1","area_id":"南京市","actualArea":"江苏省 南京市","create_user":user_name,"begin_time":begin_time}
    s.headers = operHeader
    r = s.post(operURL, json=oper_param)
    # 操作成功后,将header还原
    s.headers = header_base
    return r

#退勤
def tuiqin(yearmonth,end_time):
    operHeader['Referer'] = Origin + 'attp/attendance/initAttForJsp'
    oper_param = {"staff_code":user_name,"yearmonth":yearmonth,"project_id":"-1","projectname":" ","projectcode":"-1","area_id":"南京市","actualArea":"江苏省 南京市","create_user":user_name,"end_time":end_time}
    s.headers = operHeader
    r = s.post(operURL, json=oper_param)
    # 操作成功后,将header还原
    s.headers = header_base
    return r

#获取HH:MM样式的随机时间
def randmontime(beginHH,endHH,beginMM,endMM):
    HH = random.randint(beginHH, endHH)
    MM = random.randint(beginMM, endMM)
    if(HH<10):
        HHstr = '0' + str(HH)
    else:
        HHstr = str(HH)
    if (MM < 10):
        MMstr = '0' + str(MM)
    else:
        MMstr = str(MM)
    return HHstr + ':' + MMstr

if __name__ == '__main__':
    flag = init()
    print(flag)
    if flag:
        todaystr = today.strftime("%Y-%m-%d")
        chuqintime = randmontime(7,8,15,45)
        tuiqintime = randmontime(19,20,30,59)
        login()
        print(chuqin(todaystr,chuqintime).text)
        print(tuiqin(todaystr,tuiqintime).text)