交叉编译golang程序

本文演示了在ubuntu16.04上交叉编译golang程序的过程

配置本地golang环境

$ wget https://dl.google.com/go/go1.10.2.linux-amd64.tar.gz

$ tar zxvf go1.10.2.linux-amd64.tar.gz

$ sudo mv go /usr/local/

$ echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.profile
$ echo 'export GOROOT=/usr/local/go' >> ~/.profile
$ echo 'export GOPATH=~/go' >> ~/.profile      #v2ray的工作目录,down下来的源码和编译生成的文件均在这里

$ source ~/.profile && echo $GOPATH

$ go version
go version go1.10.2 linux/amd64

交叉编译golang程序

$ go get -u v2ray.com/core/...
$ go get -u v2ray.com/ext/...

$ go install v2ray.com/ext/tools/build/vbuild

$ $GOPATH/bin/vbuild -os=linux -arch=arm

编译完成的程序位于$GOPATH目录下. 继续阅读

Django parse a json/dictionary reminder on template

如果想在一个Django template里引入json, 正确的template写法为

python的views.py的写法

# -*- coding: utf-8 -*-
from django.shortcuts import render
from django.http import Http404, HttpResponse

def get_product(request):
    context1 = {}
    context1['env'] = 'production'
    context1['mydict'] = {"key1":"value1", "key2":"value2", "key3":"value3", "key4":"value4"}
    context1['days'] = [1,2,3]

    if request.GET.get("item") == "ec2" or not request.GET.get("item"):
        return render(request, 'main_content.html', context1)
    else:
        raise Http404()

main_content.html使用extends引入index.html

{% extends 'index.html' %}
    {% block content %}
        {% for day in days %}
        <li>day: {{ day }}</li>
        {% endfor %}
    {% endblock %}

index.html使用include引入sidebar.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <div class="header">My page</div>
    <div class="content">
        <div class="sidebar">

            <ul>
                {% if env == 'production' %}
                    {% include 'production.sidebar.html' %}
                {% elif env == 'development' %}
                    {% include 'development.sidebar.html' %}
                {% elif env == 'staging' %}
                    {% include 'staging.sidebar.html' %}
                {% elif env == 'legacy' %}
                    {% include 'legacy.sidebar.html' %}
                {% endif %}
            </ul>

        </div>
        <div class="maincontent">

            {% block content %} {% endblock %}

        </div>
    </div>
</body>
</html>

继续阅读

Python读取配置文件模块ConfigParser

1,ConfigParser模块简介
假设有如下配置文件,需要在Pyhton程序中读取

$ cat config.ini
[db]
db_port = 3306
db_user = root
db_host = 127.0.0.1
db_pass = xgmtest

[SectionOne]
Status: Single
Name: Derek
Value: Yes
Age: 30
Single: True

[SectionTwo]
FavoriteColor = Green
[SectionThree]
FamilyName: Johnson

[Others]
Route: 66

如何在Python中读取呢

>>> import ConfigParser
>>> Config = ConfigParser.ConfigParser()
>>> Config
<ConfigParser.ConfigParser instance at 0x00BA9B20>
>>> Config.read("config.ini")
['config.ini']
>>> Config.sections()
['db', 'Others', 'SectionThree', 'SectionOne', 'SectionTwo']
>>> Config.get("db", "db_host")
'127.0.0.1'
>>> Config.getint("db", "db_port")
3306

继续阅读

使用pytest进行批量测试

Pytest是python的一个测试模块,可以编写一些简单的测试用例,也可以用来进行一些批量测试,下面简单介绍一下。

1,安装

$ pip install -U pytest
or
$ easy_install -U pytest

$ py.test --version

2,基础语法

#!/usr/bin/python
import pytest

def func(x):
    return x + 1

def test_answer():    #测试用例,方法需要以test_开头
    assert func(3) == 5

运行之

$ py.test 2.py    #注意需要用py.test命令来运行之

稍微复杂一点儿的写法:

#!/usr/bin/python
import pytest

class TestClass:

    def test_one(self):
        x = "this"
        assert "h" in x

    def test_two(self):
        x = 3
        assert x > 2

继续阅读

Python tips – 轻松转换列表与字符串

There are a few useful tips to convert a Python list (or any other iterable such as a tuple) to a string for display.

First, if it is a list of strings, you may simply use join this way:

>>> mylist = ['spam', 'ham', 'eggs']
>>> print ', '.join(mylist)
spam, ham, eggs

Using the same method, you might also do this:

>>> print '\n'.join(mylist)
spam
ham
eggs

However, this simple method does not work if the list contains non-string objects, such as integers.

If you just want to obtain a comma-separated string, you may use this shortcut:

>>> list_of_ints = [80, 443, 8080, 8081]
>>> print str(list_of_ints).strip('[]')
80, 443, 8080, 8081

Or this one, if your objects contain square brackets:

>>> print str(list_of_ints)[1:-1]
80, 443, 8080, 8081

Finally, you may use map() to convert each item in the list to a string, and then join them:

>>> print ', '.join(map(str, list_of_ints))
80, 443, 8080, 8081
>>> print '\n'.join(map(str, list_of_ints))
80
443
8080
8081

python遍历文件脚本实例

自己写的一个Python遍历文件脚本,对查到的文件进行特定的处理。没啥技术含量,但是也记录一下吧。

#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import os
import shutil
dir = "/mnt/Packages"
class Packages:
    def __init__(self,srcdir,desdir):
        self.sdir=srcdir
        self.ddir=desdir
    def check(self):
        print('program start...')
        for dirpath, dirnames, filenames in os.walk(self.sdir):   #遍历文件
            for filename in filenames:
                thefile=os.path.join(dirpath,filename)            #文件的绝对地址
                try:
                    if os.path.splitext(thefile)[1]=='.rpm':      #筛选.rpm格式的文件
                        #print('Fount rpm package: ' + thefile)
                        if 'inspuer' in os.popen('rpm -qpi ' + thefile).read().rstrip():
                            print('Found error package: ' + thefile)
                            shutil.copy(thefile, self.ddir)  #将错误文件复制到desdir目录
                            f = open('list.txt', 'a')    #将错误文件列表写入到list.txt
                            f.write(filename + '\n')
                            f.close()
                except IOError, err:
                    print err
                    sys.exit()

if __name__ == '__main__':
    dir=Packages('/mnt/cdrom','/mnt/erpm')   #源目录为/mnt/cdrom,目标目录为/mnt/erpm
    dir.check()
第 1 页,共 7 页123456...最旧 »