[範例] 縮網址程式

看板Python作者 (戀愛循環)時間18年前 (2006/08/24 23:48), 編輯推噓3(302)
留言5則, 3人參與, 最新討論串1/1
看了0rz.net上的perl模組,就用類似的思維寫了一個小程式當練習嚕~ 使用了httplib的模組與0rz.net互動 python在直譯環境下也可以操作物件 這個功能真的很強大! 如果沒有這個功能 就得用telnet來模擬http client 那就很心酸了... 其實大部分的code都是看別人的範例再拼揍起來的 有些地方感覺寫得有點冗 如果大家有什麼建議改進的地方都提出討論吧^^ 互相切搓求進步咩~ 目前的用法是 *.py <長網址> [,<長網址>,<長網址> ...] 會印出短網址 *.py -r <短網址> 會印出長網址 *.py -f <存放網址的檔案> #一行一個網址 可以反查多行 #!/usr/local/bin/python import httplib, urllib,re,sys from optparse import OptionParser def makeShorter(arg): #By the time i wrote the script,0rz.net won't accept # a long url which shorter than 12 characters. if len(arg) <= 12: print 'This URL is already short enough. (%s)' % arg return params = urllib.urlencode({'url':arg}) headers = {"Content-type": "application/x-www-form-urlencoded", "Accept":"text/plain"} conn = httplib.HTTPConnection("0rz.net:80") conn.request("POST","/create.php",params,headers) response = conn.getresponse() data = response.read() conn.close() #Generlly ,an url location which is longer than 13 characters is acceptable #to 0rz.net but there are still some exceptions. if data == 'This URL is already short enough.\n': print '%s (%s)' % (data[:-1] ,arg) return m = parser.search(data) if options.ispair: print '%s %s' % (m.group(1),arg) else: print m.group(1) return def makeLonger(url): encodeOnly = re.compile(r'(\w{5})') Orznet = re.compile(r'(http://)+0rz.net/(\w{5})') m = encodeOnly.match(url) m2 = Orznet.search(url) if m != None : resource = m.group(1) url2 = "http://0rz.net/$resource" elif m2 != None : resource = m2.group(2) url2 = url else: print "Invaild URL. (%s)" % url return conn = httplib.HTTPConnection("0rz.net") conn.request("GET","/%s" % resource) res = conn.getresponse() if None != res: loc = res.getheader("location") conn.close() if options.ispair: print '%s %s' % (url2,loc) else: print loc return MSG_USAGE = "usage: %prog [-p] [-l] [-f <fileName>] url1 [, url2...]" optParser = OptionParser (MSG_USAGE) optParser.add_option("-p","--pair",action="store_true",dest="ispair" , default=False,help = "make long and short url to be a pair in one lin") optParser.add_option("-f","--file", action="store", dest="fileName", help="Read long URLs from a file and make shorters.") optParser.add_option("-r","--reverse",action="store_true" ,dest="isReverse", default=False, help="Reverse the short URLs to original longers.") options,args = optParser.parse_args() if options.isReverse: for arg in args: makeLonger(arg) if options.fileName != None: f = open (options.fileName,"r") for line in f.readlines(): line = line[:-1] makeLonger(line) else: parser = re.compile(r'(http://0rz.net/\w{5})') for arg in args: makeShorter(arg) if options.fileName != None: f = open (options.fileName,"r") for line in f.readlines(): line = line[:-1] makeShorter(line) -- 希望沒有bug了不然很糗.. -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.113.91.47

08/25 19:39, , 1F
推一個
08/25 19:39, 1F

08/27 02:12, , 2F
可以用 urllibr 取代 httplib, 程式碼會少滿多的 ..
08/27 02:12, 2F

08/27 02:13, , 3F
更正, urllib
08/27 02:13, 3F

09/05 22:36, , 4F
真的可以減少很多耶 ^^ 感謝指教 ~~
09/05 22:36, 4F

09/05 22:37, , 5F
看來是自己笨了 XD
09/05 22:37, 5F
文章代碼(AID): #14xSfZ8d (Python)
文章代碼(AID): #14xSfZ8d (Python)