使用python改变文件夹中的代码文件编码

2017-1-18 python

现状:

项目以前在windows底下写的使用的编码是gbk

在mac os底下用xcode显示中文乱码

解决方式:

    把项目底下文件的编码改为utf-8编码

使用工具:

    python

代码如下:

   

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

import os

class CodeConvert :

    def __init__(self, path, dcode, scode) :

        self.file_lists = []

        if os.path.exists(path) :

            self.parent_path = path

        else :

            self.parent_path = ""

        self.dest_code = dcode

        self.src_code = scode

        self.exts = [".cpp", ".c", ".h"]

    def ListFiles(self) :

        for root, dirs, files in os.walk(self.parent_path) :

            for name in files :

                self.file_lists.append(os.path.join(root, name))

    def Convert2DestCode(self) :

        for file in self.file_lists:

            ext = os.path.splitext(file)[1]

            ext = ext.lower()

            if ext in self.exts :

                self.Convert(file)

    def Convert(self, file) :

        try :

            print file

            f = open(file)

            buf = f.read()

            f.close()

            u1 = buf.decode(self.src_code)

            utf1 = u1.encode(self.dest_code)

            f = open(file, 'w+')

            f.write(utf1)

            f.close()

        except :

            print "except" + file


def main() :

    convert = CodeConvert('/Users/xxx/Desktop/xxx', 'utf8', 'gbk')

    convert.ListFiles()

    convert.Convert2DestCode()

标签: python utf8 gbk xcode

评论(0) 浏览(1239)

python利用py2exe生成的exe窗口一闪而过解决办法

2016-12-1 python

利用py2exe把python脚本生成exe后,运行程序一闪而过

窗口提示找不到模块例如xlsxwriter

原因是xlsxwriter库使在egg里面,因此py2exe生成时没办法导入

解决办法:

把xlsxwriter解压到工程目录文件夹中,再重新生成下就可以了

标签: python py2exe xlsxwriter

评论(0) 浏览(2344)

win10 createprocess等执行批处理窗口闪退

2016-10-26 C++

32位程序本来在win7 64位系统上运行没问题,后来系统升级到win10时,原先使用createprocess等执行的批处理程序启动不了

后来把程序编译成64位后就可以了!

通过测试,发现原来是win10系统上的32位cmd.exe启动不起来,而32位系统虽然是调用c:\windows\system32里面的cmd.exe,

实际上调用的是c:\windows\sysWow64里面的cmd.exe,具体可以查看如下网址解释:

http://www.cnblogs.com/hbccdf/p/dllchecktoolandsyswow64.html

当然如果要在32位系统调用system32里面的cmd.exe,微软也有提供接口实现!

Wow64DisableWow64FsRedirection:具体调用方式参考:

https://msdn.microsoft.com/zh-tw/library/aa365743(v=vs.85).aspx


标签: win10 createprocess 批处理

评论(0) 浏览(3302)

vs2008编译出错C2813 #import is not supported with /MP

2016-10-25 C++

用vs2008编译别人项目时候出错,报错码c2813  #import is not supported with /MP

上网找了下原因大致是import不支持并发编译;

具体错误解释https://msdn.microsoft.com/zh-cn/library/bb384890.aspx

解决办法:在项目属性的c/c++命令行加上额外参数/MP1 使用单处理器编译

标签: vs2008 C2813 MP

评论(0) 浏览(4194)

python yield 使用说明

2016-10-15 python

标签: python yield

评论(0) 浏览(1294)

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) 浏览(1305)

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

2016-10-12 C++

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

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

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

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

但是openprocess打开返回不为NULL

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

标签: VC OpenProcess

评论(0) 浏览(1366)

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) 浏览(1548)

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) 浏览(1372)

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) 浏览(2372)

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