[ Content | Sidebar ]

Archives for bookmark

ip2long python版

def ip2long1(ip):
    hexn = ''.join(["%02X" % long(i) for i in ip.split('.')])
    return long(hexn, 16)
 
def ip2long2(ip):
    lngip = 0
    for q in ip.split('.'):
        lngip = (lngip << 8) | int(q)
    return lngip
 
if __name__ == '__main__':
    print ip2long1("173.230.156.208")
    print ip2long2("173.230.156.208")
Copy Code 

———–
post by gmail~

平衡点算法备忘

原文:http://www.javaeye.com/topic/600079
各种语言,各种算法,下面总结成python

#!/usr/bin/env python
 
def bl1(nums):
    for i in xrange(1,len(nums) - 1):
        if sum(nums[:i]) == sum(nums[i+1:]):
           return i, nums[i]
    return None
 
def bl2(nums):
    total = sum(nums)
    left = 0
    for i in xrange(1,len(nums) - 1):
        left += nums[i-1] 
        if (left == total - left - nums[i]):
            return i, nums[i]
    return None
 
def bl3(nums):
    head_sum = 0
    end_sum = 0
    head = 0
    end = len(nums) - 1 
    add_head = True
    add_end = True
 
    while head < end:
        if add_head:
            head_sum += nums[head]
 
        if add_end:
            end_sum += nums[end]
 
        if head_sum < end_sum:
            head += 1
            add_head = True
            add_end = False
        elif head_sum > end_sum:
            end -= 1
            add_head = False
            add_end = True
        else:
            head += 1
            end -= 1
            add_head = True
            add_end = True
 
    if head_sum == end_sum: 
        return head, nums[head]
    else:
        return None
 
test = [1,3,5,7,8,25,4,20]
print bl1(test)
print bl2(test)
print bl3(test)
Copy Code 

———–
post by gmail~

install proxy65 on centOS 5

再次对centos的python版本表示无语

wget http://www.python.org/ftp/python/2.6.6/Python-2.6.6.tar.bz2
tar jxvf Python-2.6.6.tar.bz2
cd Python-2.6.6
./configure --prefix=/opt/python26
make
make install
 
cd ..
#试过10.2.0版本,不能用。。。
wget http://tmrc.mit.edu/mirror/twisted/Twisted/10.1/Twisted-10.1.0.tar.bz2
tar jxvf Twisted-10.1.0.tar.bz2
cd Twisted-10.1.0
/opt/python26/bin/python setup.py install
 
cd ..
wget http://www.zope.org/Products/ZopeInterface/3.3.0/zope.interface-3.3.0.tar.gz
tar zxvf zope.interface-3.3.0.tar.gz
cd zope.interface-3.3.0
/opt/python26/bin/python setup.py install
 
cd ..
wget http://proxy65.googlecode.com/files/Proxy65-1.2.0.tar.gz
tar zxvf Proxy65-1.2.0.tar.gz
cd Proxy65-1.2.0
/opt/python26/bin/python setup.py install
 
#OK
/opt/python26/bin/python /opt/python26/bin/twistd proxy65 --jid=proxy.xxx.com --secret=secret --rhost=127.0.0.1 --rport=5347 --proxyips=127.0.0.1:7777
Copy Code 

———–
post by gmail~

ubuntu下安装proxy65

proxy65安装时报错:
exceptions.ImportError: No module named words.protocols.jabber

文档里只说python setup.py install就OK了
解决:
sudo apt-get install python-twisted

———–
post by gmail~

使用gsasl模拟smtp认证

使用gsasl模拟smtp认证

gsasl -a username --service=smtp -m LOGIN --verbose -c smtp.sina.com 25
Trying `smtp.sina.com'...
220 irxd5-187.sinamail.sina.com.cn ESMTP
EHLO [127.0.0.1]
250-irxd5-187.sinamail.sina.com.cn
250-8BITMIME
250-SIZE 83886080
250-AUTH PLAIN LOGIN
250 AUTH=PLAIN LOGIN
EHLO [127.0.0.1]
250-irxd5-187.sinamail.sina.com.cn
250-8BITMIME
250-SIZE 83886080
250-AUTH PLAIN LOGIN
250 AUTH=PLAIN LOGIN
AUTH LOGIN
334 VXNlcm5hbWU6
eHh4eAo=
334 UGFzc3dvcmQ6
Enter password: 
bGFuc2VkZWh1YQ==
235 #2.0.0 OK Authenticated
客户端认证完成(服务器可信)……
输入应用程序数据(用 EOF 结束输入):
^C
Copy Code 

试了一下sina,163,qq,都只支持PLAIN LOGIN两种验证方式。。

———–
post by gmail~