독도 광고 모금 캠페인


'Library/Python'에 해당되는 글 5건

  1. 2009/05/26 permutations
  2. 2008/10/21 Look-up secondary domain by using alphabet permutation.
  3. 2008/10/08 Generate .exe by using ironpython
  4. 2008/10/06 python test suite example
  5. 2008/04/17 handling cookies with httplib2 and urlib2
2009/05/26 00:07

permutations



from __future__ import generators


def xcombinations(items, n):
    if n==0: yield []
    else:
        for i in range(len(items)):
            for cc in xcombinations(items[:i]+items[i+1:],n-1):
                yield [items[i]]+cc


def xpermutations(items):
    return xcombinations(items, len(items))

if __name__=='__main__':
    '''
    for p in xpermutations(['0','1','2','3']):
        print (''.join(p))
    '''

    arr = xpermutations(['0','1','2','3'])

    print('indices[24][4]={')
    for item in arr:
        form = '{%s,%s,%s,%s},' % (item[0], item[1], item[2], item[3])
        print(form)
    print('}')
       

compatible with python 3.x+
Trackback 0 Comment 0
2008/10/21 18:48

Look-up secondary domain by using alphabet permutation.

from socket import gethostbyname

alp = map(chr, range(97,123)) # a-z
suffix = '.xxxx.net'
prefix = [a+b+c for a in alp for b in alp for c in alp]

f = file('result.txt', 'w+')
cnt  = 0
for p in prefix:
    hostname = p + suffix
    cnt += 1
    if cnt % 100 == 0:
        print cnt, hostname
    try:
        ip = gethostbyname(hostname)
        f.write(hostname + ' : ' + ip + '\n')
        print hostname
    except:
        continue

f.close()   

어렴풋이 본 서브도메인.. 주소가 생각나지 않을 때 사용했다.



Trackback 0 Comment 0
2008/10/08 09:13

Generate .exe by using ironpython

ironpython 으로 .exe를 생성할 수 있다. ironpython이 윈도우용만 존재하는 것이 아니라 리눅스에서도 이것이 가능한데(리눅스에서는 .net 환경으로 mono라는 것이 존재한다), 리눅스에서 만든 .exe를 윈도우즈에서 실행하니 잘 안되더라. 아무튼.. .exe만드는 방법.

# compiler.py
from IronPython.Hosting import PythonCompiler

from System.Collections.Generic import List
sources = List[str]()
sources.Add('hello.py')
outfile = 'hello.exe'

compiler = PythonCompiler(sources, outfile)
compiler.Compile()


IronPython 참조 에러시 다음 추가:
import clr
clr.AddReferenceByPartialName("IronPython")
Trackback 0 Comment 0
2008/10/06 10:36

python test suite example


import unittest

class TestUtility(unittest.TestCase):
    def testExtractFileNm(self):
        self.assertEquals(1, 1)
       

def suite():
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(TestUtility))
    return suite

if __name__=='__main__':
    unittest.TextTestRunner(verbosity=2).run(suite())
   

Trackback 0 Comment 0
2008/04/17 00:15

handling cookies with httplib2 and urlib2

#!/usr/bin/env python
import urllib
import httplib2

http = httplib2.Http()
url = 'http://www.example.com/login'
body = {'USERNAME': 'foo', 'PASSWORD': 'bar'}

# POST
headers = {'Content-type': 'application/x-www-form-urlencoded'}
response, content = http.request(url, 'POST', headers=headers, body=urllib.urlencode(body))


# GET
headers = {'Cookie': response['set-cookie']}
url = 'http://www.example.com/home'
response, content = http.request(url, 'GET', headers=headers)
Trackback 0 Comment 0