分类
python

python ip地址排序

方法一:

ip_list = ['192.168.1.100', '192.168.10.3', '192.168.8.1']
ip_list.sort(lambda x,y: cmp(''.join( [ i.rjust(3, '0') for i in x.split('.')] ), ''.join( [ i.rjust(3, '0') for i in y.split('.')] ) ) )

结果

['192.168.1.100', '192.168.8.1', '192.168.10.3']

方法二:

转换成 int 型,用 int 来比较

import struct
import socket

def ip2int(addr):
    return struct.unpack("!I", socket.inet_aton(addr))[0]

def int2ip(addr):
    return socket.inet_ntoa(struct.pack("!I", addr))

ip_list = ['192.168.1.100', '192.168.10.3', '192.168.8.1']
# 构建 int_ip:ip 形式的 key:value, 并对 key 排序

ip_unsorted_dict = {}
for ip in ip_list:
    int_ip = ip2int(ip)
    ip_unsorted_dict[int_ip] = ip

keys = ip_unsorted_dict.keys()
keys.sort()
ip_sorted_list=[]
for key in keys:
    ip_sorted_list.append(ip_unsorted_dict[key])

print ip_sorted_list

结果:

['192.168.1.100', '192.168.8.1', '192.168.10.3']

参考:
http://blog.csdn.net/hong201/article/details/3119519

分类
python

python raw_input用map转换为 list

raw_input是用来提示输入的

把输入的字符以转化为list

>>> input_words = raw_input(‘input some words: ‘)

input some words: shang hai zi lai shui lai zi hai shang.

>>> input_words

‘shang hai zi lai shui lai zi hai shang.’

使用 map 将数据转换为 list

>>> input_list = map(str, input_words.split())
>>> input_list
[‘shang’, ‘hai’, ‘zi’, ‘lai’, ‘shui’, ‘lai’, ‘zi’, ‘hai’, ‘shang.’]

分类
python

python发送post, put请求

发送post,没什么可说的

import urllib
import urllib2

url = 'http://example.com/...'
values = { 'productslug': 'bar','qty': 'bar' }
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
result = response.read()
print result

发送 json 格式 post,不需要 urllib 进行 urlencode,但是需要 json.dumps(),同时需要发送头信息包含content-type

import urllib2
import json

url = 'http://example.com/...'
values = {"q":{"has_problems": "yes"}}
data = json.dumps(values)
req = urllib2.Request(url, data, {'Content-Type': 'application/json'})
response = urllib2.urlopen(req)
result = response.read()
print result

发送 json 格式 put 请求,只多加一行

import urllib2
import json

url = 'http://example.com/...'
values = {"q":{"has_problems": "yes"}}
data = json.dumps(values)
req = urllib2.Request(url, data, {'Content-Type': 'application/json'})
req.get_method = lambda:"PUT"
response = urllib2.urlopen(req)
result = response.read()
print result

使用 httplib 发送 PUT 请求

update_data为 dict 类型
def postUpdate(update_data):
    path = "/api/posts/" + str(update_data['post_id'])
    params = json.dumps(update_data)
    conn = httplib.HTTPConnection("mypost.jpuyy.com", 80)                                                                                                                 
    conn.set_debuglevel(1)
    conn.request("PUT", path, params, headers)
    response = conn.getresponse()
    result = response.read()
    return result
分类
python

python map函数对list转换

results = [‘1’, ‘2’, ‘3’]
变为
results = [1, 2, 3]

使用

results = map(int, results)

反过来转换使用

results = map(str, results)
分类
python

python print颜色

定义一个class

class bcolors:
    HEADER = '\033[95m'           # 粉色
    OKBLUE = '\033[94m'           # 蓝色
    OKGREEN = '\033[92m'          # 绿色
    WARNING = '\033[93m'          # 黄色
    FAIL = '\033[91m'             # 红色
    BOLD = '\033[1m'              # 粗体
    ENDC = '\033[0m'              # 结束

不管显示哪种颜色,最后都需要用end结束,不然会一直渲染。

print bcolors.WARNING + "Warning: No active frommets remain. Continue?" + bcolors.ENDC

参考:
http://stackoverflow.com/questions/287871/print-in-terminal-with-colors-using-python

其他颜色可以参考

#!/usr/bin/perl
print "0\t\033[0m coloured! \033[m\n";
print "1\t\033[1m coloured! \033[m\n";
print "4\t\033[4m coloured! \033[m\n";
print "7\t\033[7m coloured! \033[m\n";
print "31\t\033[31m coloured! \033[m\n";
print "32\t\033[32m coloured! \033[m\n";
print "33\t\033[33m coloured! \033[m\n";
print "34\t\033[34m coloured! \033[m\n";
print "35\t\033[35m coloured! \033[m\n";
print "36\t\033[36m coloured! \033[m\n";
print "37\t\033[37m coloured! \033[m\n";
print "38\t\033[38m coloured! \033[m\n";
print "39\t\033[39m coloured! \033[m\n";
print "40\t\033[40m coloured! \033[m\n";
print "41\t\033[41m coloured! \033[m\n";
print "42\t\033[42m coloured! \033[m\n";
print "43\t\033[43m coloured! \033[m\n";
print "44\t\033[44m coloured! \033[m\n";
print "45\t\033[45m coloured! \033[m\n";
print "46\t\033[46m coloured! \033[m\n";
print "47\t\033[47m coloured! \033[m\n";
print "90\t\033[90m coloured! \033[m\n";
print "91\t\033[91m coloured! \033[m\n";
print "92\t\033[92m coloured! \033[m\n";
print "93\t\033[93m coloured! \033[m\n";
print "94\t\033[94m coloured! \033[m\n";
print "95\t\033[95m coloured! \033[m\n";
print "96\t\033[96m coloured! \033[m\n";
print "97\t\033[97m coloured! \033[m\n";
print "98\t\033[98m coloured! \033[m\n";
print "99\t\033[99m coloured! \033[m\n";
print "100\t\033[100m coloured! \033[m\n";
print "101\t\033[101m coloured! \033[m\n";
print "102\t\033[102m coloured! \033[m\n";
print "103\t\033[103m coloured! \033[m\n";
print "104\t\033[104m coloured! \033[m\n";
print "105\t\033[105m coloured! \033[m\n";
print "106\t\033[106m coloured! \033[m\n";
print "107\t\033[107m coloured! \033[m\n";
print "108\t\033[108m coloured! \033[m\n";