http proxying python and Mechanize

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kyz Creig
    New Member
    • Aug 2011
    • 1

    http proxying python and Mechanize

    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

    Code:
    #!/usr/bin/python
    
    import mechanize
    import time
    c = chr(124)
    ref = "http://kdice.com/r?id=45079336"
    USE_PROXY = False
    USE_SCARAB = False
    br = mechanize.Browser()
    
    kevent = "http://kdice.com:80/gwt/EventService"
    kgame = "http://kdice.com:80/gwt/GameService"
    
    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
    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
    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"
    
    from mechanize import Browser,Request,urlopen,CookieJar,RobustFactory
    import cookielib
    
    f = open('C:\\twitter.txt')
    twitterlist = f.readlines()
    
    if USE_PROXY:
        f = open('C:\\proxies.txt')
        proxylist = f.readlines()
    
    if USE_SCARAB:
      br.set_proxies({"http":"localhost:8008"})
    elif USE_PROXY:
      br.set_proxies({"http":proxylist})
    
    for twit in twitterlist:
      cj = CookieJar()
      br.set_cookiejar(cj)
      br.set_handle_robots(False)
    
      r2 = br.open(ref)
    
      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')]
      res = br.open(kgame,str(gwtservice))
    
      br.addheaders = [('Content-Type',  "text/x-gwt-rpc; charset=UTF-8")]
    #  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')]
      br.addheaders = [('Referer',kgame)]
      r = br.open(kevent,gwtevent)
    
    #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|
    
    #twitter oath working fine
      namepass = twit.split("|")
      r = br.open("http://kdice.com:80/twitterOauth")
      print namepass[0],namepass[1]
      br.select_form(nr=0)
      print br.viewing_html()
      br.form["session[username_or_email]"] = namepass[0]
      br.form["session[password]"] = namepass[1][:-1]
      br.submit()
    #r = br.open(kevent,gwtmain+gwtevent)
    #  print r.read()
      print br.response().read()
      res = br.open(kgame,str(gwtservice))
      print res.read()

    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|3n99y vyb7f
    forgottea|993fa tb2j4
    hinatamuc|bay9b 64ngf
    tabibitob|x76q1 7m228
    antillesa|47mnj 37911

    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
    Last edited by Kyz Creig; Aug 31 '11, 03:57 AM. Reason: more info
Working...