Posts Tagged ‘Coding’

2010/01/19

其实 Python 中一般都是直接用 urllib.urlopen() 来抓取网页内容或者模拟登陆等操作,但是 GAE 出于安全考虑不可以用 urlopen 操作,取而代之的就是 urlfetch.fetch()。fetch() 函数、参数(via):

fetch(url, payload=None, method=GET, headers={}, allow_truncated=False, follow_redirects=True, deadline=None)

Python语言: GAE urlfetch登录人人
01 def login_renren(self):
02     login_url = ‘http://passport.renren.com/PLogin.do’
03     login_data = urllib.urlencode(
04         {
05          ‘domain’:‘renren.com’,
06          ‘email’:  renren_username,
07          ‘password’: renren_passwd,
08          ‘origURL’:‘http://home.renren.com/Home.do’,
09          })
10     result = urlfetch.fetch(
11         url = login_url,
12         payload = login_data,
13         method = urlfetch.POST,
14         headers = {‘Cookie’:make_cookie_header(cookie),
15                    ‘Content-Type’:‘application/x-www-form-urlencoded’,
16                    ‘User-Agent’:‘Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6′ },
17         follow_redirects = False)

继续学习 GAE。

Related posts

Tags: ,,,.
2009/10/09

其实就是《A Byte of Python》里面一个例子程序,拿来练练手而已,没啥技术含量。

打包压缩程序用的是 7-Zip,安装后安装目录里有一个命令行版的 7z.exe,添加压缩文件的参数是 a;自动删除旧备份文件的方法很山寨很暴力,直接 listdir 备份目录下的文件,然后删除第一个,也就是最旧的一个,凑合吧。

Python语言: Codee#7002
01 #Python 备份 Fx 配置并自动删除旧备份
02 import os
03 import time
04
05 source = r’C:\FxProfiles’
06 target_dir = r’C:\FxBackup’
07 target = target_dir + os.sep + time.strftime(‘%Y%m%d%H%M%S’) + ‘.zip’
08 newzip = time.strftime(‘%Y%m%d%H%M%S’) + ‘.zip’
09 zip_command = "7z a %s %s" % (target, ‘ ‘.join(source))
10 oldzip = os.listdir(target_dir)
11 if newzip > oldzip:
12     os.remove(target_dir + os.sep + oldzip[0])
13     print ‘Del OK’
14
15 if os.system(zip_command) == 0:
16     print ‘Successful backup to’, target
17 else:
18     print ‘Backup FAILED’

山寨之极!不过还是玩的不亦乐乎,Python 很有搞头。

Related posts

Tags: ,.
2009/09/24

Do the analysis and design.

Start implementing with a simple version.

Test and debug it.

Use it to ensure that it works as expected.

Now,add any features that you want and continue to repeat the Do-Start-Test-Use cycle as many times as required.

Remember,’Software is grown,not built’.

—-A Byte of Python.

Related posts

Tags: ,.

前一个项目基本上算是完结。抽空总结一下这个项目的一些收获,以后写东西时候留心避免以前犯过的错误,吸取些许经验。

  1. 变量定义类型。自己添加的变量要注意类型。虽然可以参考程序已有的变量类型,不过还要通盘考虑自己变量的实际应用。比如这次,弄了个变量 String 类型,在前期确实很方便,但是后期就需要转 Int 型,比前期 String 用的还多……不想返工,只好 StrToIntDef(x,0) 的用,开销反而更大。
  2. 添加的函数要注意应用范围,不能添加一个函数实现了需要的功能而影响到其他范围内函数的功能。简单的方法就是添加函数时制定它的工作域
  3. 善用 Trim() 对字符串过滤。
  4. 标志变量要注意用完后返回初值
  5. 充分考虑判断条件组合可能带来的不同结果。
  6. 阅读程序的能力,程序已有同样功能的变量、函数不要重复制造轮子
  7. 注意已有的程序注释

Related posts

Tags: .