[经验总结]用Python的urllib库提交WEB表单
上次实现的校园网IP网关登录器其中一个关键部分就是提交登录网页的表单,下面是我的Python实现代码:import urllib2,urllib1. class EntryDemo( Frame ):2. """Demonstrate Entrys and Event binding"""3. 4. chosenrange = 25. url_login="http://.../ipgw/ipgw.ipgw/"6. uid = '' #用户名7. password = '' # 密码8. operation = '' #操作9. range = '2' # 范围10. the_page = '' # WEB服务器返回页面11. # 表单的INPUT 值一定要记得填齐全12. def login(self):13. values = {14. 'uid' : self.uid,15. 'password' : self.password,16. 'operation' : self.operation,17. 'range' : self.range, # 1:国际 2:国内18. 'timeout':'0'19. }20. postdata = urllib.urlencode(values) # 表单值编码21. req = urllib2.Request(self.url_login, postdata)# 服务器请求22. response = urllib2.urlopen(req) 23. self.the_page = response.read()import urllib2,urllib1. class EntryDemo( Frame ):2. """Demonstrate Entrys and Event binding"""3. 4. chosenrange = 25. url_login="http://.../ipgw/ipgw.ipgw/"6. uid = '' #用户名7. password = '' # 密码8. operation = '' #操作9. range = '2' # 范围10. the_page = '' # WEB服务器返回页面11. # 表单的INPUT 值一定要记得填齐全12. def login(self):13. values = {14. 'uid' : self.uid,15. 'password' : self.password,16. 'operation' : self.operation,17. 'range' : self.range, # 1:国际 2:国内18. 'timeout':'0'19. }20. postdata = urllib.urlencode(values) # 表单值编码21. req = urllib2.Request(self.url_login, postdata)# 服务器请求22. response = urllib2.urlimport urllib2,urllib1. class EntryDemo( Frame ):2. """Demonstrate Entrys and Event binding"""3. 4. chosenrange = 25. url_login="http://.../ipgw/ipgw.ipgw/"6. uid = '' #用户名7. password = '' # 密码8. operation = '' #操作9. range = '2' # 范围10. the_page = '' # WEB服务器返回页面11. # 表单的INPUT 值一定要记得填齐全12. def login(self):13. values = {14. 'uid' : self.uid,15. 'password' : self.password,16. 'operation' : self.operation,17. 'range' : self.range, # 1:国际 2:国内18. 'timeout':'0'19. }20. postdata = urllib.urlencode(values) # 表单值编码21. req = urllib2.Request(self.url_login, postdata)# 服务器请求22. response = urllib2.url
页:
[1]