By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,399 Members | 886 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,399 IT Pros & Developers. It's quick & easy.

http proxying python and Mechanize

P: 1
Hi I am trying to make this code run through a new proxy ip each time it runs (ideally using a .txt list of proxies, I know they have to be http). How would I go about doing that, thanks! (also I am willing to buy actual http proxies in order for this to work if thats what I have to do. I just want to the code to work, so far it hasn't).

running vista, python 2.7 and mechanize

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/python
  2.  
  3. import mechanize
  4. import time
  5. c = chr(124)
  6. ref = "http://kdice.com/r?id=45079336"
  7. USE_PROXY = False
  8. USE_SCARAB = False
  9. br = mechanize.Browser()
  10.  
  11. kevent = "http://kdice.com:80/gwt/EventService"
  12. kgame = "http://kdice.com:80/gwt/GameService"
  13.  
  14. gwtevent   = "5" + c+"0"+c+"4"+c+"http://kdice.com/gwt/"+c+"709ECEA46C2A1C2E2A6E201A883C7074"+c+"org.iogc.client.EventService"+c+"getEvents"+c+"1"+c+"2"+c+"3"+c+"4"+c+"0"+c
  15. gwtservice = "5"+c+"0"+c+"7"+c+"http://kdice.com/gwt/"+c+"1A671DB120885A115DE783B967FE5EB2"+c+"org.iogc.kdice.client.KGameService"+c+"startSession"+c+"I"+c+"java.lang.String"+c+"paypal"+c+"1"+c+"2"+c+"3"+c+"4"+c+"2"+c+"5"+c+"6"+c+"3"+c+"7"+c
  16. gwtleave   = "5"+c+"0"+c+"4"+c+"http://kdice.com/gwt/"+c+"1A671DB120885A115DE783B967FE5EB2"+c+"org.iogc.kdice.client.KGameService"+c+"leave"+c+""+c+"1"+c+"2"+c+"3"+c+"4"+c+"0"
  17.  
  18. from mechanize import Browser,Request,urlopen,CookieJar,RobustFactory
  19. import cookielib
  20.  
  21. f = open('C:\\twitter.txt')
  22. twitterlist = f.readlines()
  23.  
  24. if USE_PROXY:
  25.     f = open('C:\\proxies.txt')
  26.     proxylist = f.readlines()
  27.  
  28. if USE_SCARAB:
  29.   br.set_proxies({"http":"localhost:8008"})
  30. elif USE_PROXY:
  31.   br.set_proxies({"http":proxylist})
  32.  
  33. for twit in twitterlist:
  34.   cj = CookieJar()
  35.   br.set_cookiejar(cj)
  36.   br.set_handle_robots(False)
  37.  
  38.   r2 = br.open(ref)
  39.  
  40.   br.addheaders = [('User-Agent', 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.100 Safari/534.30')]
  41.   res = br.open(kgame,str(gwtservice))
  42.  
  43.   br.addheaders = [('Content-Type',  "text/x-gwt-rpc; charset=UTF-8")]
  44. #  br.addheaders = [('User-Agent', 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.100 Safari/534.30')]
  45.   br.addheaders = [('Referer',kgame)]
  46.   r = br.open(kevent,gwtevent)
  47.  
  48. #5|0|7|http://kdice.com/gwt/|1A671DB120885A115DE783B967FE5EB2|org.iogc.kdice.client.KGameService|startSession|I|java.lang.String|nodice4u|1|2|3|4|2|5|6|3|7|
  49.  
  50. #twitter oath working fine
  51.   namepass = twit.split("|")
  52.   r = br.open("http://kdice.com:80/twitterOauth")
  53.   print namepass[0],namepass[1]
  54.   br.select_form(nr=0)
  55.   print br.viewing_html()
  56.   br.form["session[username_or_email]"] = namepass[0]
  57.   br.form["session[password]"] = namepass[1][:-1]
  58.   br.submit()
  59. #r = br.open(kevent,gwtmain+gwtevent)
  60. #  print r.read()
  61.   print br.response().read()
  62.   res = br.open(kgame,str(gwtservice))
  63.   print res.read()
  64.  

ideally I'd like for it to select the following proxy from a .txt file and use that to connect via mechanize as it uses login information from a .txt file. My understanding of mechanize sort of breaks down when it comes to proxies so I'm in real quandary here, any help would be much obliged.

If you want to try running the script yourself feel free to use this twitter logins in a .txt file.

elffirezd|3n99yvyb7f
forgottea|993fatb2j4
hinatamuc|bay9b64ngf
tabibitob|x76q17m228
antillesa|47mnj37911

The reason it must work with proxies has to do with the referral system we're currently testing and debugging. Thank you.

Oh and if necessary I can probably provide some http proxies too.

edit: Right now it runs perfectly without proxies but of course the referral only works once. so we want to test it out the referral system multiple times by using proxy IPs
Aug 31 '11 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.