Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think the best approach is to mock a subset of <code>httplib.HTTPConnection</code> (call the resulting class <code>mockcon</code> for concreteness in the following) and add a handler using it and subclassing <code>HTTPHandler</code> (to use in <code>build_opener</code> -- the subclassing means it can <em>replace</em> <code>HTTPHandler</code> that <code>build_opener</code> uses by default):</p> <pre><code>class MockHTTPHandler(urllib2.HTTPHandler): def http_open(self, req): return self.do_open(mockcon, req) </code></pre> <p>The mockcon class must supply the methods <code>do_open</code> call -- several can be dummies (i.e. accept and ignore arbitrary args and kwds and do nothing):</p> <pre><code>set_debuglevel _set_tunnel request </code></pre> <p>(may be interested in the 2nd arg of <code>request</code>, as it gives the "selector" part of the URL).</p> <p>The <code>__init__</code> method of <code>mockcon</code> gets the host part of the URL as the first arg (i.e., first after <code>self</code> of course) and should ignore following kwds (used to set a timeout).</p> <p>The <code>get_response</code> method of <code>mockcon</code> (no args, beyond of course <code>self</code>) must return an http response object -- i.e., a file-like readable object which also has attributes <code>.msg</code>, <code>.status</code>, and <code>.reason</code>, and a method <code>get_full_url()</code> to return the URL.</p> <p>You could use an actual <code>httplib.HTTPResponse</code> instance for the latter role, but you must initialize it with one mock/dummy arg that has a <code>makefile</code> argument (ignores its args and kwds and returns whatever), and, right after initializing it, reset its <code>.fp</code> argument to be a <code>rb</code> open file giving exactly the bytes that a real HTTP response would receive on its socket.</p> <p>I think that building a full-fledged mock for the whole <code>urllib2.urlopen</code> call might be simpler than this attempt to reuse most of the functionality of <code>urllib2</code> (and <code>httplib</code> which it uses internally), though perhaps not quite as simple as the "local web server" approach which you appear to think is more work. But it's worth considering all the three approaches (the mock would surely be most-lightweight/fast in operation, the local web server slowest... and would also require somehow modifying the URLs by prefixing an <code>http://localhost:someport/</code> to them, of course).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload