作者Morneau (恋爱循环)
看板Python
标题[范例] 缩网址程式
时间Fri Aug 25 00:05:55 2006
看了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
1F:推 ericsk:推一个 08/25 19:39
2F:推 xcycl:可以用 urllibr 取代 httplib, 程式码会少满多的 .. 08/27 02:12
3F:→ xcycl:更正, urllib 08/27 02:13
4F:推 Morneau:真的可以减少很多耶 ^^ 感谢指教 ~~ 09/05 22:36
5F:→ Morneau:看来是自己笨了 XD 09/05 22:37