I see, Paramiko&#39;s socket dependency isn&#39;t inverted, so you are unable to provide an alternate implementation of that interface.<div><br></div><div>I don&#39;t know if this will work, but you could try overriding the socket module&#39;s import, replacing it with your own implementation of the same interface.</div>
<div><br></div><div>Here are some discussions that suggest it may work, as long as you override the import before Paramiko gets imported:</div><div><a href="http://stackoverflow.com/questions/1096216/override-namespace-in-python">http://stackoverflow.com/questions/1096216/override-namespace-in-python</a></div>
<div><a href="http://stackoverflow.com/questions/3012473/how-do-i-override-a-python-import">http://stackoverflow.com/questions/3012473/how-do-i-override-a-python-import</a><br><br><div class="gmail_quote">On Tue, Jul 31, 2012 at 4:17 PM, Jathan McCollum <span dir="ltr">&lt;<a href="mailto:jathan@gmail.com" target="_blank">jathan@gmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Ok, a little background...<div><br></div><div>On the heels of BlackHat and DefCon I discovered a new tool called FakeNet (<a href="http://practicalmalwareanalysis.com/fakenet/" target="_blank">http://practicalmalwareanalysis.com/fakenet/</a>) that is intended to run on Windows XP. It allows you to hijack all socket connections received by the system, and has an embedded Python 2.7.3 interpreter. I want to try using this for a single-system dummy test network.</div>

<div><br></div><div>The catch is, because of the custom socket I/O, the _socket.so module was intentionally excluded. In order to do socket calls you must import FakeNet. Here is a simple example:</div><div><br></div><div>

<div>def FN_NewConnection(context):</div><div>    &quot;&quot;&quot;Simple echo server that prints Python version upon connection&quot;&quot;&quot;</div><div>    import sys</div><div>    FakeNet.sendData(context, sys.version)</div>

<div>    while True:</div><div>        bytes = FakeNet.recvData(context, 4096)</div><div>        if bytes == &quot;&quot;:</div><div>            break</div><div>        else:</div><div>            FakeNet.sendData(context, bytes)</div>

<div><div><br></div><div>It&#39;s a greatly simplified interface. It handles the buildup/teardown of the session, and all you have to do is worry about send/recv.</div><div><br></div><div>The device allows for custom modules. I am trying to mock an SSH server and actually negotiate with the client. I want to use this for... You guessed it... testing interaction with SSH endpoints without actually having to connect to anything.</div>

<div><br></div><div>For this I want to use Paramiko, but the problem is that Paramiko imports the socket module all over the place, so I wanted to see if I could force it (somehow) to use FakeNet&#39;s send/recv calls, and just mock the rest.</div>

<div><br></div><div>Steve, thanks for the reminder of dependency inversion. :)</div><div><br></div><div><div><div class="h5"><div class="gmail_quote">On Mon, Jul 30, 2012 at 6:18 PM, Andrew Kou <span dir="ltr">&lt;<a href="mailto:andrew.kou@gmail.com" target="_blank">andrew.kou@gmail.com</a>&gt;</span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>Jathan,</div><div><br></div>What requirements do you have of the mocked object?<span><font color="#888888"><div>
<br></div></font></span><div><span><font color="#888888">- Andrew<br><div><br></div><div><br></div></font></span><div><br><div class="gmail_quote"><div>On Mon, Jul 30, 2012 at 4:38 PM, Jathan McCollum <span dir="ltr">&lt;<a href="mailto:jathan@gmail.com" target="_blank">jathan@gmail.com</a>&gt;</span> wrote:<br>



</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div>I have a need to mock the socket module, but without actually importing it first. I know some pretty sophisticated mocking can be done with Mox, but it requires that you import the real module into the namespace first.<div>




<br></div><div>I am doing some work on an embedded system that doesn&#39;t have _socket.so, which means I can&#39;t actually import the socket module, but I need to do some mocking of connections.</div><div><br></div><div>




Anyone have any pointers? Thanks in advance!</div><span><font color="#888888"><div><div><br></div>-- <br>Jathan.<br>--<br>
</div>
</font></span><br></div></div><div>_______________________________________________<br>
socal-piggies mailing list<br>
<a href="mailto:socal-piggies@lists.idyll.org" target="_blank">socal-piggies@lists.idyll.org</a><br>
<a href="http://lists.idyll.org/listinfo/socal-piggies" target="_blank">http://lists.idyll.org/listinfo/socal-piggies</a><br>
<br></div></blockquote></div><br></div></div>
<br>_______________________________________________<br>
socal-piggies mailing list<br>
<a href="mailto:socal-piggies@lists.idyll.org" target="_blank">socal-piggies@lists.idyll.org</a><br>
<a href="http://lists.idyll.org/listinfo/socal-piggies" target="_blank">http://lists.idyll.org/listinfo/socal-piggies</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div></div></div><span class="HOEnZb"><font color="#888888">-- <br>Jathan.<br>--<br>
</font></span></div></div></div>
<br>_______________________________________________<br>
socal-piggies mailing list<br>
<a href="mailto:socal-piggies@lists.idyll.org">socal-piggies@lists.idyll.org</a><br>
<a href="http://lists.idyll.org/listinfo/socal-piggies" target="_blank">http://lists.idyll.org/listinfo/socal-piggies</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br><div>Steve Wedig</div><a href="http://www.linkedin.com/in/wedig" target="_blank">http://www.linkedin.com/in/wedig</a><br>
</div>