Top-rated recipes tagged "network"http://code.activestate.com/recipes/tags/network/top/2012-05-15T18:45:34-07:00ActiveState Code RecipesSend an HTML email with embedded image and plain text alternate (Python) 2006-01-29T18:40:36-08:00darrin massenahttp://code.activestate.com/recipes/users/1987292/http://code.activestate.com/recipes/473810-send-an-html-email-with-embedded-image-and-plain-t/ <p style="color: grey"> Python recipe 473810 by <a href="/recipes/users/1987292/">darrin massena</a> (<a href="/recipes/tags/network/">network</a>). </p> <p>HTML is the method of choice for those wishing to send emails with rich text, layout and graphics. Often it is desirable to embed the graphics within the message so recipients can display the message directly, without further downloads.</p> <p>Some mail agents don't support HTML or their users prefer to receive plain text messages. Senders of HTML messages should include a plain text message as an alternate for these users.</p> <p>This recipe sends a short HTML message with a single embedded image and an alternate plain text message.</p> Run asynchronous tasks using coroutines (Python) 2010-08-06T16:16:20-07:00Arnau Sanchezhttp://code.activestate.com/recipes/users/4173270/http://code.activestate.com/recipes/577129-run-asynchronous-tasks-using-coroutines/ <p style="color: grey"> Python recipe 577129 by <a href="/recipes/users/4173270/">Arnau Sanchez</a> (<a href="/recipes/tags/coroutine/">coroutine</a>, <a href="/recipes/tags/event/">event</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/gobject/">gobject</a>, <a href="/recipes/tags/gtk/">gtk</a>, <a href="/recipes/tags/gui/">gui</a>, <a href="/recipes/tags/network/">network</a>, <a href="/recipes/tags/nonblocking/">nonblocking</a>, <a href="/recipes/tags/pygtk/">pygtk</a>). Revision 20. </p> <p>This recipe shows a simple, transparent (and hopefully pythonic) way of running asynchronous tasks when writing a event-driven application (i.e. GUI). The aim is to allow a programmer to write time-consuming functions (usually IO-bound, but not only) with sequential-looking code, instead of scattering the logic over a bunch of callbacks. We will take advantage of the coroutines introduced in Python 2.5 (see <a href="http://www.python.org/dev/peps/pep-0342" rel="nofollow">http://www.python.org/dev/peps/pep-0342</a>). </p> <p>The goal: wouldn't it be great if we could write something like this?</p> <pre class="prettyprint"><code>def myjob(entry, arg1, arg2, arg3): result1 = function_that_takes_eons_to_complete(arg1, arg2) result2 = another_function_that_downloads_a_big_really_big_file(result1, arg3) entry.set_text("The result is: %d" % result2) def on_start_button___clicked(button, entry): myjob(entry, 1, 2, 3) ... gtk.main() </code></pre> <p>Indeed, but we can't! The GUI will hang until the job is done and the user will be rightfully angry. Coroutines to the rescue: the absolute minimal change we can make to this code is transforming <em>myjob</em> into a coroutine and yield every time we do blocking stuff:</p> <pre class="prettyprint"><code>def myjob(entry, arg1, arg2, arg3): result1 = yield some_task(arg1, arg2) result2 = yield some_other_task(result1, arg3) entry.set_text("The result is: %d" % result2) def on_start__clicked(button, entry): start_job(myjob(entry, 1, 2, 3)) </code></pre> <p><em>some_task</em> and <em>some_other_task</em> are here the asynchronous implementation of the sequential tasks used in the first fragment, and <em>start_job</em> the wrapper around the coroutine. Note that we still have to implement non-blocking versions of the tasks, but they are usually pretty generic (wait some time, download a file, ...) and can be re-used. If you happen to have a CPU-bound function or even a IO-bound code you cannot split (<em>urllib2</em> anyone?), you can always use a generic threaded task (granted, the whole point of using co-routines should be avoiding threads, but there is no alternative here).</p> <p>At the end, all the plumbing we need to make it work is just 1 function: <em>start_job</em> (wrapper around the job to manage the flow of the coroutine). The rest of the code -two asynchronous tasks (<em>sleep_task</em>, <em>threaded_task</em>) and a demo app- are shown solely as an example.</p> Simple Web Crawler (Python) 2011-01-31T21:57:58-08:00James Millshttp://code.activestate.com/recipes/users/4167757/http://code.activestate.com/recipes/576551-simple-web-crawler/ <p style="color: grey"> Python recipe 576551 by <a href="/recipes/users/4167757/">James Mills</a> (<a href="/recipes/tags/crawler/">crawler</a>, <a href="/recipes/tags/network/">network</a>, <a href="/recipes/tags/parsing/">parsing</a>, <a href="/recipes/tags/web/">web</a>). Revision 2. </p> <p>NOTE: This recipe has been updated with suggested improvements since the last revision.</p> <p>This is a simple web crawler I wrote to test websites and links. It will traverse all links found to any given depth.</p> <p>See --help for usage.</p> <p>I'm posting this recipe as this kind of problem has been asked on the Python Mailing List a number of times... I thought I'd share my simple little implementation based on the standard library and BeautifulSoup.</p> <p>--JamesMills</p> urrlib2 opener for SSL proxy (CONNECT method) (Python) 2005-11-16T15:04:54-08:00Alessandro Budaihttp://code.activestate.com/recipes/users/2668504/http://code.activestate.com/recipes/456195-urrlib2-opener-for-ssl-proxy-connect-method/ <p style="color: grey"> Python recipe 456195 by <a href="/recipes/users/2668504/">Alessandro Budai</a> (<a href="/recipes/tags/network/">network</a>). Revision 2. </p> <p>This small module builds an urllib2 opener that can be used to make a connection through a proxy using the http CONNECT method (that can be used to proxy SSLconnections). The current urrlib2 seems to not support this method.</p> get the IP address associated with a network interface (linux only) (Python) 2005-08-11T13:30:18-07:00paul cannonhttp://code.activestate.com/recipes/users/2551140/http://code.activestate.com/recipes/439094-get-the-ip-address-associated-with-a-network-inter/ <p style="color: grey"> Python recipe 439094 by <a href="/recipes/users/2551140/">paul cannon</a> (<a href="/recipes/tags/network/">network</a>). </p> <p>Uses the Linux SIOCGIFADDR ioctl to find the IP address associated with a network interface, given the name of that interface, e.g. "eth0". The address is returned as a string containing a dotted quad.</p> Mini Fake DNS server (Python) 2006-04-18T17:41:59-07:00Francisco Santoshttp://code.activestate.com/recipes/users/2853535/http://code.activestate.com/recipes/491264-mini-fake-dns-server/ <p style="color: grey"> Python recipe 491264 by <a href="/recipes/users/2853535/">Francisco Santos</a> (<a href="/recipes/tags/network/">network</a>). Revision 4. </p> <p>Minimal python dns server, it only replies with a selected ip in an A record</p> Port Forwarding (Python) 2012-05-15T18:45:34-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/483730-port-forwarding/ <p style="color: grey"> Python recipe 483730 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/network/">network</a>). </p> <p>The title of this recipe contains the two words that gave the inspiration for the writing of what appears below. Port forwarding -- think about it.</p> socket.recv -- three ways to turn it into recvall (Python) 2005-04-05T13:47:57-07:00John Nielsenhttp://code.activestate.com/recipes/users/135654/http://code.activestate.com/recipes/408859-socketrecv-three-ways-to-turn-it-into-recvall/ <p style="color: grey"> Python recipe 408859 by <a href="/recipes/users/135654/">John Nielsen</a> (<a href="/recipes/tags/network/">network</a>). Revision 5. </p> <p>An issue with socket.recv is how to know when you are done receiving data. A TCP stream guarantees the bytes will not arrive out of order or be sent more than once. But you do not know the size of the data that will be sent to you. 100 bytes could be sent as group of 10 bytes or maybe in one shot. Ultimately, this means you have to use a loop in some fashion until you know it is done.</p> <p>The basic recv returns an empty string when the socket is disconnected. From that you can build a simple loop that will work as long as the sender manages to disconnect the socket at the appropriate time. However, there could be situations where a local error will mask as a clean shutdown or maybe a close() is never called.</p> <p>Three very basic methods are shown below that try to fix that problem. They use either a time-based, end marker, or size of payload method. Since you cannot be sure just what you are going to receive, you have to be careful that you get enough of a message to determine the size of payload or end marker.</p> <p>I updated the recv_size method to allocate data in larger chunks if it gets a large stream of data, which can increase performance.</p> Python portscanners (Python) 2004-07-13T16:14:01-07:00Foo Bearhttp://code.activestate.com/recipes/users/181960/http://code.activestate.com/recipes/286240-python-portscanners/ <p style="color: grey"> Python recipe 286240 by <a href="/recipes/users/181960/">Foo Bear</a> (<a href="/recipes/tags/network/">network</a>). </p> <p>Those are a couple of multithreaded portscanners, the second one use the Queue module.</p> Send an HTML email with embedded image and plain text alternate (Python) 2011-06-12T18:38:14-07:00soham vaghelahttp://code.activestate.com/recipes/users/4178285/http://code.activestate.com/recipes/577751-send-an-html-email-with-embedded-image-and-plain-t/ <p style="color: grey"> Python recipe 577751 by <a href="/recipes/users/4178285/">soham vaghela</a> (<a href="/recipes/tags/network/">network</a>). </p> <p>HTML is the method of choice for those wishing to send emails with rich text, layout and graphics. Often it is desirable to embed the graphics within the message so recipients can display the message directly, without further downloads.</p> <p>Some mail agents don't support HTML or their users prefer to receive plain text messages. Senders of HTML messages should include a plain text message as an alternate for these users.</p> <p>This recipe sends a short HTML message with a single embedded image and an alternate plain text message.</p> Pyscanlogger - Python Port scan detector (Python) 2010-03-17T07:27:15-07:00Anandhttp://code.activestate.com/recipes/users/760763/http://code.activestate.com/recipes/576690-pyscanlogger-python-port-scan-detector/ <p style="color: grey"> Python recipe 576690 by <a href="/recipes/users/760763/">Anand</a> (<a href="/recipes/tags/network/">network</a>, <a href="/recipes/tags/programs/">programs</a>, <a href="/recipes/tags/security/">security</a>). Revision 5. </p> <p>A pure Python program to detect network port scanning attacks. Currently logs different TCP port scans. Can run in the background like a daemon and log attacks to a log file.</p> Chat server & client using select.select (Python) 2007-09-28T08:57:53-07:00Anandhttp://code.activestate.com/recipes/users/760763/http://code.activestate.com/recipes/531824-chat-server-client-using-selectselect/ <p style="color: grey"> Python recipe 531824 by <a href="/recipes/users/760763/">Anand</a> (<a href="/recipes/tags/network/">network</a>). Revision 3. </p> <p>This recipe demos how to write a simple command line chat server &amp; client using multiplexing using select.select. The server uses select call to multiplex multiple clients and the client uses it to multiplex command line &amp; socket I/O.</p> Standard Error Redirector (Python) 2006-03-20T01:53:49-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/475149-standard-error-redirector/ <p style="color: grey"> Python recipe 475149 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/network/">network</a>). Revision 2. </p> <p>This recipe was designed for remotely receiving bug reports. It was written after participating in a programming contest where feedback was not helpful. The concept presented here is a step towards working with Python remotely. As sys.stderr is replaced in this recipe, so sys.stdin and sys.stdout can also be redirect to an alternate source (such as sockets connected to another computer).</p> SMTP Mailsink (Python) 2005-10-11T11:00:32-07:00Adam Feuerhttp://code.activestate.com/recipes/users/2512995/http://code.activestate.com/recipes/440690-smtp-mailsink/ <p style="color: grey"> Python recipe 440690 by <a href="/recipes/users/2512995/">Adam Feuer</a> (<a href="/recipes/tags/network/">network</a>). </p> <p>This little class starts up an SMTP server which acts as an email sink, collecting all received emails destined for any address. All emails are routed to a Portable Unix Mailbox file. This is very handy for testing applications that send email. It runs in its own thread, so you can easily use it from a test fixture to collect your emails and verify them for correctness.</p> Simple stoppable server using socket timeout (Python) 2005-06-09T00:35:47-07:00Dirk Holtwickhttp://code.activestate.com/recipes/users/636691/http://code.activestate.com/recipes/425210-simple-stoppable-server-using-socket-timeout/ <p style="color: grey"> Python recipe 425210 by <a href="/recipes/users/636691/">Dirk Holtwick</a> (<a href="/recipes/tags/network/">network</a>). </p> <p>The usual Python HTTP server never stops. Since there is the timout option in the socket module, there is an easy way to do a clean shutdown of a running server.</p> Remote Shutdown on Windows (Python) 2004-12-24T01:40:56-08:00Fadly Tabranihttp://code.activestate.com/recipes/users/2143621/http://code.activestate.com/recipes/360649-remote-shutdown-on-windows/ <p style="color: grey"> Python recipe 360649 by <a href="/recipes/users/2143621/">Fadly Tabrani</a> (<a href="/recipes/tags/network/">network</a>). </p> <p>Shuts down or reboots a remote computer.</p> Wake On Lan (Python) 2004-12-23T14:19:25-08:00Fadly Tabranihttp://code.activestate.com/recipes/users/2143621/http://code.activestate.com/recipes/358449-wake-on-lan/ <p style="color: grey"> Python recipe 358449 by <a href="/recipes/users/2143621/">Fadly Tabrani</a> (<a href="/recipes/tags/network/">network</a>). Revision 3. </p> <p>Switches on remote computers using WOL.</p> Simple FTP directory synch (Python) 2005-11-16T21:34:24-08:00EyePulphttp://code.activestate.com/recipes/users/2138976/http://code.activestate.com/recipes/327141-simple-ftp-directory-synch/ <p style="color: grey"> Python recipe 327141 by <a href="/recipes/users/2138976/">EyePulp</a> (<a href="/recipes/tags/network/">network</a>). Revision 4. </p> <p>This script filled the need to have a scheduled directory synch occur via FTP. I also realized I could use it to clean out a directory without much effort. There are probably more robust examples out there, but this one should be easily modifiable for FTP newbies. It uses Sets to speed up finding missing files from the local directory.</p> Strip attachments from an email message (Python) 2004-08-26T06:08:02-07:00anthony baxterhttp://code.activestate.com/recipes/users/98198/http://code.activestate.com/recipes/302086-strip-attachments-from-an-email-message/ <p style="color: grey"> Python recipe 302086 by <a href="/recipes/users/98198/">anthony baxter</a> (<a href="/recipes/tags/network/">network</a>). </p> <p>This recipe shows a simple approach to using the Python email package to strip out attachments and file types from an email message that might be considered dangerous. This is particularly relevant in Python 2.4, as the email Parser is now much more robust in handling mal-formed messages (which are typical for virus and worm emails)</p> simplest useful HTTPS with basic proxy authentication (Python) 2005-12-28T17:27:47-08:00John Nielsenhttp://code.activestate.com/recipes/users/135654/http://code.activestate.com/recipes/301740-simplest-useful-https-with-basic-proxy-authenticat/ <p style="color: grey"> Python recipe 301740 by <a href="/recipes/users/135654/">John Nielsen</a> (<a href="/recipes/tags/network/">network</a>). Revision 4. </p> <p>This is just about the most simple snippet of how to do proxy authentication with SSL using python. The current httplib only supports ssl through a proxy _without_ authentication. This example does basic proxy auth that a lot of proxy servers can support. This will at least give someone an idea of how to do it and then improve it and incorporate it however they want.</p>