<?xml version="1.0" encoding="UTF-8" ?>
<rdf:RDF xmlns="http://usefulinc.com/ns/doap#" xmlns:foaf="http://xmlns.com/foaf/0.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"><Project><name>collective.anonymousbrowser</name>
<shortdesc>A zope.testbrowser extension with useragent faking and proxy abilities</shortdesc>
<description>Introduction
=============
This Yet-Another-Mechanize implementation aims to give the developper those new
features:

 - It can be proxified
 - It does proxy balancing
 - It fakes user agent ``by default``
 - It does not handle robots ``by default``
 - There is a 'real" modification which uses an underlying moz repl server to
   control a distance firefox instance

It uses sys.prefix/etc/config.ini with a part [collective.anonymousbrowser] for its settings::

     [collective.anonymousbrowser]
     proxies=
     ; for a mozrepl server
     host = localhost 
     port = 4242
     firefox = /path/To/Firefox
     ff-profile = /path/to/FFprofile

This file is generated at the first run without proxies. It s your own to feed it with some open proxies.

Of course, it can take another configuration file, please see the __init__ method.

TODO
=====
- lxml integration, maybe steal z3c.etestbrowser


Tests and Handbook
======================

First, we need to instantiate the sources where we come from::

    &gt;&gt;&gt; from collective.anonymousbrowser.browser import Browser, FF2_USERAGENT

 
User Agent
------------
Oh, my god, we have a brand new user agent by default::

        &gt;&gt;&gt; br = Browser()
        &gt;&gt;&gt; br.open('http://localhost:45678')
        &gt;&gt;&gt; FF2_USERAGENT in br.contents
        True                
        &gt;&gt;&gt; br2 = Browser('http://localhost:45678')
        &gt;&gt;&gt; FF2_USERAGENT in br2.contents
        True


Proxy mode
------------
But, we want to be anonymous, and we ll set a proxy
To define those proxies, just just a config.ini file like::

    [collective.anonymousbrowser]
    proxies =
        host1:port
        host2:port

When the browser has many proxies defined, it will circly through those ones.
But, it will not use the same host indefinitivly, just set the ``proxy_max_use`` argument::

    &gt;&gt;&gt; from StringIO import StringIO
    &gt;&gt;&gt; from tempfile import mkstemp
    &gt;&gt;&gt; __, config = mkstemp()
    &gt;&gt;&gt; open(config, 'w').write("""[collective.anonymousbrowser]
    ... proxies =
    ...     127.0.0.1:45675
    ...     127.0.0.1:45676
    ...     127.0.0.1:45677
    ...     127.0.0.1:45678
    ...     127.0.0.1:45679
    ...     """)
    &gt;&gt;&gt; b = Browser(config=config, proxy_max_use=3)
    &gt;&gt;&gt; b._config._sections
    {'collective.anonymousbrowser': {'__name__': 'collective.anonymousbrowser', 'proxies': '\n127.0.0.1:45675\n127.0.0.1:45676\n127.0.0.1:45677\n127.0.0.1:45678\n127.0.0.1:45679'}}
    &gt;&gt;&gt; b.proxies
    ['127.0.0.1:45675', '127.0.0.1:45676', '127.0.0.1:45677', '127.0.0.1:45678', '127.0.0.1:45679']
    &gt;&gt;&gt; b.proxified
    True
    &gt;&gt;&gt; b.open('http://localhost:45678')
    &gt;&gt;&gt; 'Host: localhost:45678' in b.contents
    True
    &gt;&gt;&gt; b._lastproxy['count'] == 1 and b._lastproxy['proxy'] in [0,1,2,3,4]
    True

We can have a normal unproxified browser too ::

    &gt;&gt;&gt; b1 = Browser(proxify=False)
    &gt;&gt;&gt; b1.proxified
    False

Next thing to verify is that we have our pseudo-random loop running
First thing is we will choose 2 times the 2nd proxy, then the third
And of course, we will set the mocker to change the proxy at each row.::

    &gt;&gt;&gt; import mocker
    &gt;&gt;&gt; import random
    &gt;&gt;&gt; mocked = mocker.Mocker()
    &gt;&gt;&gt; custom_random_int = mocked.replace('random.randint')
    &gt;&gt;&gt; custom_random_int(0, 4)
    &lt;mocker.Mock ...
    &gt;&gt;&gt; mocked.result(2)
    &gt;&gt;&gt; custom_random_int(0,1)
    &lt;mocker.Mock ...
    &gt;&gt;&gt; mocked.result(0)
    &gt;&gt;&gt; custom_random_int(0, 4)
    &lt;mocker.Mock ...
    &gt;&gt;&gt; mocked.result(2)
    &gt;&gt;&gt; custom_random_int(0,1)
    &lt;mocker.Mock ...
    &gt;&gt;&gt; mocked.result(0)
    &gt;&gt;&gt; custom_random_int(0, 4)
    &lt;mocker.Mock ...
    &gt;&gt;&gt; mocked.result(2)
    &gt;&gt;&gt; custom_random_int(0,1)
    &lt;mocker.Mock ...
    &gt;&gt;&gt; mocked.result(0)
    &gt;&gt;&gt; custom_random_int(0, 4)
    &lt;mocker.Mock ...
    &gt;&gt;&gt; mocked.result(3)
    &gt;&gt;&gt; custom_random_int(0,1)
    &lt;mocker.Mock ...
    &gt;&gt;&gt; mocked.result(0)
    &gt;&gt;&gt; custom_random_int(0, 4)
    &lt;mocker.Mock ...
    &gt;&gt;&gt; mocked.result(4)
    &gt;&gt;&gt; custom_random_int(0,1)
    &lt;mocker.Mock ...
    &gt;&gt;&gt; mocked.result(0)
    &gt;&gt;&gt; custom_random_int(0, 4)
    &lt;mocker.Mock ...
    &gt;&gt;&gt; mocked.result(2)
    &gt;&gt;&gt; custom_random_int(0,1)
    &lt;mocker.Mock ...
    &gt;&gt;&gt; mocked.result(0)
    &gt;&gt;&gt; custom_random_int(0, 4)
    &lt;mocker.Mock ...
    &gt;&gt;&gt; mocked.result(1)
    &gt;&gt;&gt; custom_random_int(0,1)
    &lt;mocker.Mock ...
    &gt;&gt;&gt; mocked.result(0)
    &gt;&gt;&gt; mocked.replay()
    &gt;&gt;&gt; b = Browser('http://localhost:45678', config=config, proxy_max_use=3)
    &gt;&gt;&gt; b.open('http://localhost:45678')
    &gt;&gt;&gt; b._lastproxy
    {'count': 1, 'proxy': 2}
    &gt;&gt;&gt; b.open('http://localhost:45678')
    &gt;&gt;&gt; b._lastproxy
    {'count': 2, 'proxy': 2}
    &gt;&gt;&gt; b.open('http://localhost:45678')
    &gt;&gt;&gt; b._lastproxy
    {'count': 3, 'proxy': 2}
    &gt;&gt;&gt; b.open('http://localhost:45678')
    &gt;&gt;&gt; b._lastproxy
    {'count': 1, 'proxy': 0}
    &gt;&gt;&gt; b.open('http://localhost:45678')
    &gt;&gt;&gt; b._lastproxy
    {'count': 1, 'proxy': 3}
    &gt;&gt;&gt; b.open('http://localhost:45678')
    &gt;&gt;&gt; b._lastproxy
    {'count': 1, 'proxy': 4}
    &gt;&gt;&gt; b.open('http://localhost:45678')
    &gt;&gt;&gt; b._lastproxy
    {'count': 1, 'proxy': 2}
    &gt;&gt;&gt; b.open('http://localhost:45678')
    &gt;&gt;&gt; b._lastproxy
    {'count': 1, 'proxy': 1}
    &gt;&gt;&gt; mocked.restore()

If the proxies are dead, we remove them from the list::

    &gt;&gt;&gt; __, config = mkstemp()
    &gt;&gt;&gt; open(config, 'w').write("""[collective.anonymousbrowser]
    ... proxies =
    ...     127.0.0.1:35675
    ...     127.0.0.1:35676
    ...     127.0.0.1:35677
    ...     127.0.0.1:45678
    ...     """)
    &gt;&gt;&gt; mybrowser = Browser(config=config, proxy_max_use=3)
    &gt;&gt;&gt; mybrowser.proxies
    ['127.0.0.1:35675', '127.0.0.1:35676', '127.0.0.1:35677', '127.0.0.1:45678']
    &gt;&gt;&gt; mybrowser.open('http://localhost:45678')
    &gt;&gt;&gt; mybrowser.proxies
    ['127.0.0.1:45678']
    &gt;&gt;&gt; mybrowser.proxies = ['127.0.0.1:34785']
    &gt;&gt;&gt; mybrowser.open('http://localhost:45678')
    Traceback (most recent call last):
    ...
    Exception: There are no valid proxies left


The loop is recursion protected. If we return always the same host, so the chooser cannot choose anything else.
It will loop until it crashes or it handle the recursion::

    &gt;&gt;&gt; def randomint(a,b):
    ...     return 2
    &gt;&gt;&gt; import random; random.randint = randomint
    &gt;&gt;&gt; b2 = Browser(config=config, proxy_max_use=3)
    &gt;&gt;&gt; b2.proxy_max_use
    3
    &gt;&gt;&gt; b2._lastproxy['count']
    0
    &gt;&gt;&gt; b2.chooseProxy()
    '...
    &gt;&gt;&gt; b2._lastproxy['count']
    1
    &gt;&gt;&gt; b2.chooseProxy()
    '...
    &gt;&gt;&gt; b2._lastproxy['count']
    2
    &gt;&gt;&gt; b2.chooseProxy()
    '...
    &gt;&gt;&gt; b2._lastproxy['count']
    3
    &gt;&gt;&gt; b2.chooseProxy()
    '...
    &gt;&gt;&gt; b2.chooseProxy()
    Ho, seems we got the max wills to choose, something has gone wrong
    '127.0.0.1:35675'



..  vim:set ft=doctest:

Real Browser implementation throught mozrepl
=============================================

TODO:

    - Handle configuration with mozrunner for:

        - user agent faking
        - proxies management

First, we need to instantiate the sources where we come from::

    &gt;&gt;&gt; from collective.anonymousbrowser.real import *

In the section [collective.anonymousbrowser] of your configuration file you
can add those parameters:

    - ``host`` : host of firefox mozrepl instance
    - ``port`` : port of firefox mozrepl instance
    - ``firefox`` : path to the firefox binary
    - ``firefox-profile`` : path to the firefox profile to use


Start to use it on our little http server::

    &gt;&gt;&gt; b = Browser('http://localhost:45675')
    &gt;&gt;&gt; b.contents
    '&lt;html&gt;...&lt;pre&gt;...localhost:45675...&lt;/pre&gt;...&lt;/html&gt;'

    &gt;&gt;&gt; b.open('http://localhost:45675')
    &gt;&gt;&gt; b.contents
    '&lt;html&gt;...&lt;pre&gt;...localhost:45675...&lt;/pre&gt;...&lt;/html&gt;'

Kill any launched firefox from the browser instance with its configuration
settings::

    &gt;&gt;&gt; b.stop_ff()
    &gt;&gt;&gt; b.start_ff()
    &lt;mozrunner.runner.Firefox object at ...&gt;
    &gt;&gt;&gt; b.restart_ff()
    &lt;mozrunner.runner.Firefox object at ...&gt;

Cleanup::

    &gt;&gt;&gt; b.stop_ff()

..  vim:set ft=doctest:


HISTORY
========
0.10-&lt;0.11
-----------
- bugfix for 0.9

0.9
----
- Fix binary distributions, now with a sample decorator, mozrunner executes
  its commands in firefox directories

0.8
-----
- bugfix for js execution

0.7
-----
- bugfix: firefox is started when you call open... its better.
 
0.6
-----
- doc + bugfixes
- use of testrunner to handle firefox instance
- robustify the proxy code
- add tests

0.4
-----
-   doc + bugfixes

0.3
-----
-   adding error message

0.2
-----
-   Adding proxy fallback facility

0.1
-----
-   Initial release</description>
<homepage rdf:resource="https://svn.plone.org/svn/collective/collective.anonymousbrowser/trunk" />
<maintainer><foaf:Person><foaf:name>Mathieu Pasquet</foaf:name>
<foaf:mbox_sha1sum>61d9ae8462a118bc132971d3c3fa1bd1555d1458</foaf:mbox_sha1sum></foaf:Person></maintainer>
<release><Version><revision>0.11dev-r82630</revision></Version></release>
</Project></rdf:RDF>