python with 使用说明

2016-10-15 python

参考:http://effbot.org/zone/python-with-statement.htm

一旦知道with段用于解决什么问题,那使用with语法将会非常简单:

如下片段:

    set things up #打开一个文件或请求外部资源

    try :

        do something

    finally :

        tear things down #关闭文件或释放资源

    try-finally结构保证"tear things down"一直都会被执行,不管"do something"有没有完成


如果要经常使用这个结构,可以考虑使用如下结构:

    def controlled_execution(callback) :

        set things up

        try :

            callback(thing)

        finally :

            tear things down

    def my_fn(thing) :

        do something


    controlled_execution(my_fn)

但是看起来有点啰嗦, 也可以采用如下方式:

    def controlled_execution() :

        set things up

        try :

            yield thing

        finally :

            tear things down


    for thing in controlled_execution() :

        do something in thing

但你只要执行一次函数时,也要使用loop就有点奇怪了

python团队使用如下结构来替代:

    class controlled_execution :

        def __enter__(self) :

            set things up

            return thing

        def __exit__(self, type, value, traceback) :

            tear things down

    with controlled_execution() as thing :

        some code

当with执行时,python执行__enter__方法,返回值给thing,

然后执行some code, 不管some code执行成功与否,执行完调用__exit__方法;

如果要抑制异常抛出,可以使__exit__()返回True,如忽略TypeError:

    def __exit__(self, type, value, traceback) :

        return isinstance(value, TypeError)

打开一个文件,处理里面内容然后保证它被关闭,可以使用如下代码:

    with open("x.txt") as f :

        data = f.read()

        do something with data

    

标签: python with

评论(0) 浏览(1161)

OpenProcess 打开任务列表不存在进程返回200

2016-10-12 C++

通过OpenProcess判断进程存不存在时,

对于在进程管理器不存在的进程通过OpenProcess打开时返回不为NULL

原因是进程存在时open时没有关闭HANDLE

进程关闭后,虽然在进程管理器找不到该进程,

但是openprocess打开返回不为NULL

解决办法,保证每个openprocess后都对应closehandle

标签: VC OpenProcess

评论(0) 浏览(1208)

python 解决‘ascii‘ codec can‘t encode characters in position ordinal not in range(128)

2016-10-10 python

使用java 通过cmd调用python脚本时出现unicodeEncodeError

通过搜索说是读取文件默认是ascii而不是utf8导致的

在代码中加上如下几句即可

import sys

reload(sys)

sys.setdefaultencoding(‘utf8’)

标签: python

评论(0) 浏览(1137)

python dir_util copy_tree errno 9

2016-10-9 python

使用python2.7.10写脚本从一个文件夹复制文件到另一个文件夹时出错

错误原因在读取源文件时找不到文件描述

errno 9

但是通过python idle执行可以,后来把python更新到2.7.12版本就可以了

可能是2.7.10 bug吧

运行环境 windows 2008 64位 英文版

标签: python

评论(0) 浏览(1207)

vps ubuntu 搭建 ipsec l2tp vpn服务器

2016-9-19 vpn

由于升级了ios10,ios10不支持pptp vpn服务器连接,因此重新安装了下vpn服务器采用ipsec + xl2tpd

参考:https://raymii.org/s/tutorials/IPSEC_L2TP_vpn_with_Ubuntu_14.04.html


1.安装ppp openswan xl2tpd

sudo apt-get install openswan xl2tpd ppp lsof

2.设置防火墙,如果没设置的话连接成功后没办法接上外网

iptables -t nat -A POSTROUTING -j SNAT --to-source vps主机ip -o eth0(主机ip网卡)

修改/etc/sysctl.conf设置以下几个选项:

net.ipv4.ip_forward = 1

net.ipv4.conf.all.accept_redirects = 0

net.ipv4.conf.all.send_redirects = 0

net.ipv4.conf.default.rp_filter = 0

net.ipv4.conf.default.accept_source_route = 0

net.ipv4.conf.default.send_redirects = 0

net.ipv4.icmp_ignore_bogus_error_responses = 1

然后sudo sysctl -p应用他们

可以通过修改/etc/rc.local实现开机设置:把如下一条命令放在exit 0之前

iptables -t nat -A POSTROUTING -j SNAT --to-source vps主机ip -o eth0(主机ip网卡)

3.配置ipsec /etc/ipsec.conf

version 2 # conforms to second version of ipsec.conf specification

config setup
    dumpdir=/var/run/pluto/
    #in what directory should things started by setup (notably the Pluto daemon) be allowed to dump core?

    nat_traversal=yes
    #whether to accept/offer to support NAT (NAPT, also known as "IP Masqurade") workaround for IPsec

    virtual_private=%v4:10.0.0.0/8,%v4:192.168.0.0/16,%v4:172.16.0.0/12,%v6:fd00::/8,%v6:fe80::/10
    #contains the networks that are allowed as subnet= for the remote client. In other words, the address ranges that may live behind a NAT router through which a client connects.

    protostack=netkey
    #decide which protocol stack is going to be used.

    force_keepalive=yes
    keep_alive=60
    # Send a keep-alive packet every 60 seconds.

conn L2TP-PSK-noNAT
    authby=secret
    #shared secret. Use rsasig for certificates.

    pfs=no
    #Disable pfs

    auto=add
    #the ipsec tunnel should be started and routes created when the ipsec daemon itself starts.

    keyingtries=3
    #Only negotiate a conn. 3 times.

    ikelifetime=8h
    keylife=1h

    ike=aes256-sha1,aes128-sha1,3des-sha1
    phase2alg=aes256-sha1,aes128-sha1,3des-sha1
    # https://lists.openswan.org/pipermail/users/2014-April/022947.html
    # specifies the phase 1 encryption scheme, the hashing algorithm, and the diffie-hellman group. The modp1024 is for Diffie-Hellman 2. Why 'modp' instead of dh? DH2 is a 1028 bit encryption algorithm that modulo's a prime number, e.g. modp1028. See RFC 5114 for details or the wiki page on diffie hellmann, if interested.

    type=transport
    #because we use l2tp as tunnel protocol 
		

left=vps主机ip

#fill in server IP above leftprotoport=17/1701 right=%any rightprotoport=17/%any dpddelay=10 # Dead Peer Dectection (RFC 3706) keepalives delay dpdtimeout=20 # length of time (in seconds) we will idle without hearing either an R_U_THERE poll from our peer, or an R_U_THERE_ACK reply. dpdaction=clear # When a DPD enabled peer is declared dead, what action should be taken. clear means the eroute and SA with both be cleared.
4.设置共享密匙 /etc/ipsec.secrets

vps主机ip %any: PSK "随便字符串"

5.验证ipsec设置

ipsec verify

Checking your system to see if IPsec got installed and started correctly:
Version check and ipsec on-path                                 [OK]
Linux Openswan U2.6.38/K3.13.0-24-generic (netkey)
Checking for IPsec support in kernel                            [OK]
 SAref kernel support                                           [N/A]
 NETKEY:  Testing XFRM related proc values                      [OK]
    [OK]
    [OK]
Checking that pluto is running                                  [OK]
 Pluto listening for IKE on udp 500                             [OK]
 Pluto listening for NAT-T on udp 4500                          [OK]
Checking for 'ip' command                                       [OK]
Checking /bin/sh is not /bin/dash                               [WARNING]
Checking for 'iptables' command                                 [OK]
Opportunistic Encryption Support                                [DISABLED]
6.配置xl2tpd.conf

[global]
ipsec saref = yes
saref refinfo = 30

;debug avp = yes
;debug network = yes
;debug state = yes
;debug tunnel = yes

[lns default]
ip range = 192.168.1.20-192.168.1.100
local ip = 192.168.1.1
refuse pap = yes
require authentication = yes
;ppp debug = yes
pppoptfile = /etc/ppp/options.xl2tpd
length bit = yes
7.配置ppp /etc/ppp/options.xl2tpd 

require-mschap-v2
ms-dns 8.8.8.8
ms-dns 8.8.4.4
auth
mtu 1200
mru 1000
crtscts
hide-password
modem
name l2tpd
proxyarp
lcp-echo-interval 30
lcp-echo-failure 4

8.添加用户 /etc/ppp/chap-secrets

bob            l2tpd   DF98F09F74C06A2F             *

10.重启服务

/etc/init.d/ipsec restart 
/etc/init.d/xl2tpd restart
问题1:

Two or more interfaces found, checking IP forwarding        [FAILED]

这个可以不用管,通过echo 0 > /proc/sys/net/ipv4/ip_forward 可以解决,但是这样就没办法上外网了


问题2:

"L2TP-PSK-noNAT"[33]  #33: STATE_QUICK_R2: IPsec SA established transport mode {DPD=enabl}

拨号上去到出现上面语句后后就没相应了,原因是配置文件漏了,重新检查下配置文件就可以了

		


标签: vpn ipsec l2tp

评论(0) 浏览(1987)

健身日志

2016-9-10 健身

8.26-9.9腹肌变化,加油。。。。

IMG_5354.jpg

IMG_5370.jpg

IMG_5401.jpg

IMG_5427.jpg

IMG_5497.jpg

评论(0) 浏览(1246)

解决“应用程序无法启动,应用程序的并行配置不正确”

2016-9-10 C++

应用程序事件日志中:

“xxx.exe”的激活上下文生成失败。 找不到从属程序集 Microsoft.VC80.MFC,processorArchitecture="amd64",publicKeyToken="1fc8b3b9a1e18e3b",type="win32",version="8.0.50727.762"。 请使用 sxstrace.exe 进行详细诊断。

出现这类问题,是由于VS2008编译出来的程序文件,采用了manifest方式来指定dll文件。解决办法把相应的dll及mainifest放到程序目录底下


解决办法
在类似C:/Program Files/Microsoft Visual Studio 9/VC/redi
st/Debug_NonRedist/amd64Microsoft.VC80.DebugCRT 下找到了下列文件:

msvcm80d.dll
msvcp80d.dll
msvcr80d.dll
Microsoft.VC80.DebugCRT.manifest

把这几个文件拷贝到目标机器上,与运行程序同一文件夹


标签: MFC

评论(0) 浏览(1249)

2016-9-6

迷茫。。。。。。。。。。。。。。。。。。。。

评论(0) 浏览(953)

vps ubuntu 通过pptpd freeradius radiusclient搭建vpn服务器

2016-8-15 vpn

vps服务器使用的是digitalocean服务器。地址为https://www.digitalocean.com/

pptpd跟freeradius radiusclient配合网上教程很多,大部分教程都是大同小异,前面参考了好多配置都是死活不成功!

今天重新下载配置了下,奇迹般的可以了!后面总结出来应该是设置账号的时候设置为之错了!

新增帐号应该放在/etc/freeradius/users 里面原先的测试帐号steve的位置下面,不应该放在文件末尾

最后参考的pptpd等配置网址为:

http://www.cnblogs.com/klobohyz/archive/2012/02/04/2338675.html

http://www.cnblogs.com/klobohyz/archive/2012/02/01/2334811.html

另radius读取/usr/local/etc/radiusclient/dictionary 配置出现了不识别的类型,把如下的注释掉就可以

#

#       RFC3162 IPv6 attributes

#

#ATTRIBUTE      NAS-IPv6-Address        95      string

#ATTRIBUTE      Framed-Interface-Id     96      string

#ATTRIBUTE      Framed-IPv6-Prefix      97      ipv6prefix

#ATTRIBUTE      Login-IPv6-Host         98      string

#ATTRIBUTE      Framed-IPv6-Route       99      string

#ATTRIBUTE      Framed-IPv6-Pool        100     string

#

#       RFC6911 IPv6 attributes

#

#ATTRIBUTE      Framed-IPv6-Address     168     ipv6addr

#ATTRIBUTE      DNS-Server-IPv6-Address 169     ipv6addr

#ATTRIBUTE      Route-IPv6-Information  170     ipv6prefix



标签: vpn pptpd freeradius radiusclient

评论(0) 浏览(1196)

6.4-6.11台湾行

2016-6-16 摄影

6.4深圳-桃园机场-高雄-垦丁

6.5垦丁

6.6垦丁-高雄-台中-蓬甲夜市

6.7台中-日月潭-台中-台北

6.8故宫博物馆-淡水中学-淡水老街-士林夜市

6.9mr.j餐厅-诚品书店-微风广场-电影-101-鼎泰丰-base10夜店

6.10台北-瑞芳-十分-猫园-九份-宁夏夜市

6.11 台北-王品-桃园机场-深圳

标签: 台湾 旅游

评论(0) 浏览(1179)

Powered by EMLOG Copyright @ 狼酒 版权所有. 闽ICP备14012694号-2