1. #!/usr/bin/python 2. #-*-coding:utf-8-*- 3. 4. # 进行表单提交 小项 2008-10-09 5. 6. import httplib,urllib; #加载模块 7. 8. #定义需要进行发送的数据 9. params = urllib.urlencode({'cat_id':'6', 10. 'news_title':'标题-Test39875', 11. 'news_author':'Mobedu', 12. 'news_ahome':'来源', 13. 'tjuser':'carchanging', 14. 'news_keyword':'|', 15. 'news_content':'测试-Content', 16. 'action':'newnew', 17. 'MM_insert':'true'}); 18. #定义一些文件头 19. headers = {"Content-Type":"application/x-www-form-urlencoded", 20. "Connection":"Keep-Alive","Referer":"http://192.168.1.212/newsadd.asp?action=newnew"}; 21. #与网站构建一个连接 22. conn = httplib.HTTPConnection("192.168.1.212"); 23. #开始进行数据提交 同时也可以使用get进行 24. conn.request(method="POST",url="/newsadd.asp?action=newnew",body=params,headers=headers); 25. #返回处理后的数据 26. response = conn.getresponse(); 27. #判断是否提交成功 28. if response.status == 302: 29. print "发布成功!^_^!"; 30. else: 31. print "发布失败\^0^/"; 32. #关闭连接 33. conn.close(); |
January 9, 2010
[Python]Python學習筆記16-用python POST
[Python]Python學習筆記15- Post 到 google translate
import httplib, urllib params = urllib.urlencode({"hl": "en", "text": "this is mytest","langpair": "en|zh-CN"}) conn = httplib.HTTPConnection("translate.google.com") conn.request("POST", "/translate_t", params) r1 = conn.getresponse() print r1.read() |
python删除文件
import os 删除文件: os.remove() 删除空目录: os.rmdir() 递归删除空目录: os.removedirs() 递归删除目录和文件(类似DOS命令DeleteTree): 方法1:自力更生,艰苦创业 # Delete everything reachable from the directory named in 'top', # assuming there are no symbolic links. # CAUTION: This is dangerous! For example, if top == '/', it # could delete all your disk files. import os for root, dirs, files in os.walk(top, topdown=False): for name in files: os.remove(os.path.join(root, name)) for name in dirs: os.rmdir(os.path.join(root, name)) 方法2:前人栽树,后人乘凉 import shutil shutil.rmtree() 一行搞定 __import__('shutil').rmtree() |
获取文件夹大小的python代码
import os from os.path import join, getsize def getdirsize(dir): size = 0L for root, dirs, files in os.walk(dir): size += sum([getsize(join(root, name)) for name in files]) return size if '__name__' == '__main__': filesize = getdirsize(r'c:\windows') print 'There are %.3f' % (size/1024/1024), 'Mbytes in c:\\windows' |
[Python]Python學習筆記-Python產生標準日期格式的方法
time.strftime( ‘%Y-%m-%d %H:%M:%S’, time.localtime() )
python 的时间处理
>>> time.localtime( time.time() )
(2008, 11, 29, 21, 6, 53, 5, 334, 0)