python用法: post一个http请求, schedule一个task
来源:互联网 发布:qq软件升级 编辑:程序博客网 时间:2024/04/27 21:27
内容在http://iihero.cn上也有,这里转摘一下。
近期用空闲时间看了看python的一部分module,感觉这斯功能确实so good, so powerful.
(1) 用它post一个http请求:
import urllib,urllib2,cookielib
def post3():
# for mail.sina.com.cn
cj = cookielib.CookieJar()
url_login = 'http://mail.sina.com.cn/cgi-bin/login.cgi'
body = (('logintype','login'), ('u','username'),
('psw', '********'))
opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
#opener.addheaders = [('User-agent', 'Opera/9.23')]
opener.addheaders = [('User-agent',
'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)')]
urllib2.install_opener(opener)
req=urllib2.Request(url_login,urllib.urlencode(body))
u=urllib2.urlopen(req)
print u.read().decode('utf-8').encode('gbk')
下午,试了一下python的http 相关类的方法,用上述代码登录新浪邮箱,试了一段时间,
比较关键的是User-agent,上边两种浏览器的agent都支持。估计python默认的User-agent得不到sina.com的验证。
python写这种http method代码还是蛮方便的。
(2) 写一个定时执行任务的小东东,这里是单线程版本,要改成多线程的也容易。
#!/usr/bin/env python
#coding=utf-8
import thread, time
def task():
'''
Here we can execute some task to be scheduled every n seconds
'''
print "task doing ... ..."
def main(n):
t = time.time()
start_t = t
end_t = start_t + 60*60*72
#while (t < end_t):
while True:
task()
time.sleep(n)
t = time.time()
if __name__ == "__main__":
try:
main(5)
except KeyboardInterrupt:
print "System exit ... ... "
sys.exit(1)
#coding=utf-8
import thread, time
def task():
'''
Here we can execute some task to be scheduled every n seconds
'''
print "task doing ... ..."
def main(n):
t = time.time()
start_t = t
end_t = start_t + 60*60*72
#while (t < end_t):
while True:
task()
time.sleep(n)
t = time.time()
if __name__ == "__main__":
try:
main(5)
except KeyboardInterrupt:
print "System exit ... ... "
sys.exit(1)
- python用法: post一个http请求, schedule一个task
- Python发一个POST请求
- python构造一个http请求
- 使用python发送一个POST请求
- python 发送http post请求
- Python - Http Get Post请求
- 一个http请求工具
- 一个 Http 请求
- 一个http请求过程
- 一个ajax的Post请求
- curl 模拟一个post请求
- 单独提交一个post请求
- post请求拉一个类
- python socket模拟一个http Get请求页面
- Python 初体验 POST HTTP 请求
- python http请求post参数,json解析
- python gzip http post请求结果解压
- python发HTTP post请求上传文件
- C#破解access数据库密码方法
- 写得蛮好的linux学习笔记(转)
- 上传图片存入sql及显示
- 某可人官方网站挂马Trojan-PSW.Win32.OnLineGames.sbg
- 北京服务外包产值占全国三成
- python用法: post一个http请求, schedule一个task
- 东南亚在国际外包中扮演重要角色
- 外包或成合肥“金字招牌”
- 信雅达电子影像系统雄踞市场第一
- 英特尔大连厂采用65纳米工艺获美国政府批准
- 每个初学者都应该搞懂的问题(java)(转)
- QuickTime简史
- 破解Access(*.mdb)目前所有版本的密码
- Zeller公式及其c++实现