上次实现的校园网IP网关登录器其中一个关键部分就是提交登录网页的表单,下面是我的Python实现代码:
import urllib2,urllib 1. class EntryDemo( Frame ): 2. """Demonstrate Entrys and Event binding""" 3. 4. chosenrange = 2 5. 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,urllib 1. class EntryDemo( Frame ): 2. """Demonstrate Entrys and Event binding""" 3. 4. chosenrange = 2 5. 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,urllib 1. class EntryDemo( Frame ): 2. """Demonstrate Entrys and Event binding""" 3. 4. chosenrange = 2 5. 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