Most viewed recipes tagged "network"http://code.activestate.com/recipes/tags/network/views/2014-09-01T14:55:56-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>
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>
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>
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>
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 & client using multiplexing using select.select. The server uses select call to multiplex multiple clients and the client uses it to multiplex command line & socket I/O.</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>
When to not just use socket.close() (Python)
2005-04-08T17:31:14-07:00John Nielsenhttp://code.activestate.com/recipes/users/135654/http://code.activestate.com/recipes/408997-when-to-not-just-use-socketclose/
<p style="color: grey">
Python
recipe 408997
by <a href="/recipes/users/135654/">John Nielsen</a>
(<a href="/recipes/tags/network/">network</a>).
Revision 5.
</p>
<p>I have implemented a "broken" client/server to show how socket.shutdown can be more useful than a simple socket.close operation. (I rewrote this to hopefully be more clear)</p>
<p>The close operation is not atomic. It implicitly tries to send any remaining data in _addition_ to closing a descriptor. Splitting this close operation up with the aid of the shutdown command can help avoid bugs. It gives the server one final way to say, "something went wrong". The server would also know that the client did not end correctly, since the socket should remain open when the client finished sending data. For example, if the function exits unexpectedly and python closes the socket for you, the server would not be able to send any data back.</p>
<p>In the server below, the client and server have different ideas about what the end marker should be. The rev_end function is written so as to look for an end marker. And, as long as they agree it should work. The socket.shutdown is for when something goes wrong.</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>
Print Directly from web application to POS/EPS thermal printer (PHP)
2014-09-01T14:55:56-07:00imam feriantohttp://code.activestate.com/recipes/users/633541/http://code.activestate.com/recipes/578925-print-directly-from-web-application-to-poseps-ther/
<p style="color: grey">
PHP
recipe 578925
by <a href="/recipes/users/633541/">imam ferianto</a>
(<a href="/recipes/tags/kasir/">kasir</a>, <a href="/recipes/tags/network/">network</a>, <a href="/recipes/tags/over/">over</a>, <a href="/recipes/tags/php/">php</a>, <a href="/recipes/tags/pos/">pos</a>, <a href="/recipes/tags/printer/">printer</a>, <a href="/recipes/tags/tiketing/">tiketing</a>).
Revision 2.
</p>
<p>this php script will printout barcode label directly from the web by phpscript</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>
icmplib: library for creating and reading ICMP packets (Python)
2007-07-15T19:37:46-07:00Brett Cannonhttp://code.activestate.com/recipes/users/98030/http://code.activestate.com/recipes/409689-icmplib-library-for-creating-and-reading-icmp-pack/
<p style="color: grey">
Python
recipe 409689
by <a href="/recipes/users/98030/">Brett Cannon</a>
(<a href="/recipes/tags/network/">network</a>).
Revision 2.
</p>
<p>[co-authored with Philip E. Nu�ez]</p>
<p>Python's stdlib does not have any included library for supporting ICMP packets; both reading them or creating them. But ICMP packets are common and useful; they are used for both the traceroute and ping utilities. And thus they can be useful to control to do network diagnostics.</p>
<p>The Packet class is what is used to create and read ICMP packets. To create a packet you instantiate the class, set the header and data fields, and then call the create() method which will the string representation that can be passed to a socket.</p>
<p>To read a packet, use the Packet.parse() classmethod, which will return an instance of Packet with the fields filled out.</p>
<p>To show its use you can also see the ping() method that is included. Just use the code as a script and pass in an address to ping. Response time is printed to stdout.</p>
<p>One word of warning, though, when using this module. Raw sockets tend to require root permissions on the process. Thus you might need to use sudo to execute the Python interpreter to make this all work. ping() does drop sudo permissions as soon as it can, though, for security reasons.</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>
Genetic Algorithm in Python source code - AI-Junkie tutorial (Python)
2012-06-19T12:59:13-07:00David Adlerhttp://code.activestate.com/recipes/users/4182015/http://code.activestate.com/recipes/578128-genetic-algorithm-in-python-source-code-ai-junkie-/
<p style="color: grey">
Python
recipe 578128
by <a href="/recipes/users/4182015/">David Adler</a>
(<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/artificial/">artificial</a>, <a href="/recipes/tags/genetic/">genetic</a>, <a href="/recipes/tags/network/">network</a>, <a href="/recipes/tags/neural/">neural</a>, <a href="/recipes/tags/python/">python</a>).
Revision 5.
</p>
<p>A simple genetic algorithm program. I followed this tutorial to make the program <a href="http://www.ai-junkie.com/ga/intro/gat1.html." rel="nofollow">http://www.ai-junkie.com/ga/intro/gat1.html.</a></p>
<p>The objective of the code is to evolve a mathematical expression which calculates a user-defined target integer.</p>
<hr />
<p>KEY:</p>
<p>chromosome = binary list (this is translated/decoded into a protein in the format number --> operator --> number etc, any genes (chromosome is read in blocks of four) which do not conform to this are ignored.</p>
<p>protein = mathematical expression (this is evaluated from left to right in number + operator blocks of two)</p>
<p>output = output of protein (mathematical expression)</p>
<p>error = inverse of difference between output and target</p>
<p>fitness score = a fraction of sum of of total errors</p>
<hr />
<p>OTHER:</p>
<p>One-point crossover is used.</p>
<p>I have incorporated <strong>elitism</strong> in my code, which somewhat deviates from the tutorial but made my code more efficient (top ~7% of population are carried through to next generation)</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>
Windows network file transfers (Python)
2005-11-04T16:20:21-08:00Fadly Tabranihttp://code.activestate.com/recipes/users/2143621/http://code.activestate.com/recipes/442521-windows-network-file-transfers/
<p style="color: grey">
Python
recipe 442521
by <a href="/recipes/users/2143621/">Fadly Tabrani</a>
(<a href="/recipes/tags/network/">network</a>).
Revision 3.
</p>
<p>This module enables users on the windows platform to transfer files to remote hosts. Requires pywin32 extensions by Mark Hammond</p>
IP address conversion functions with the builtin socket module (Python)
2001-08-14T09:14:39-07:00Alex Martellihttp://code.activestate.com/recipes/users/97991/http://code.activestate.com/recipes/66517-ip-address-conversion-functions-with-the-builtin-s/
<p style="color: grey">
Python
recipe 66517
by <a href="/recipes/users/97991/">Alex Martelli</a>
(<a href="/recipes/tags/network/">network</a>).
</p>
<p>Convert dotted-quad IP addresses to long integer and back, get network and host portions from an IP address, all nice and fast thanks to the builtin socket module (with a little help from the builtin struct module, too).</p>
Hex-dump port-forwarding network proxy server (Python)
2007-04-09T17:22:07-07:00Andrew Ehttp://code.activestate.com/recipes/users/4034885/http://code.activestate.com/recipes/502293-hex-dump-port-forwarding-network-proxy-server/
<p style="color: grey">
Python
recipe 502293
by <a href="/recipes/users/4034885/">Andrew E</a>
(<a href="/recipes/tags/network/">network</a>).
Revision 5.
</p>
<p>A lightweight and portable way to hex-dump network traffic between network applications.</p>
Simple telnet session scripting (Python)
2002-09-25T19:47:17-07:00Tim Keatinghttp://code.activestate.com/recipes/users/131643/http://code.activestate.com/recipes/152043-simple-telnet-session-scripting/
<p style="color: grey">
Python
recipe 152043
by <a href="/recipes/users/131643/">Tim Keating</a>
(<a href="/recipes/tags/network/">network</a>).
Revision 2.
</p>
<p>This is an EXTREMELY simple module for scripting a telnet session. It uses abbreviated versions of the commands exported by telnetlib followed by any necessary arguments.</p>
<p>An example of use would be:</p>
<p>import telnetscript</p>
<p>script = """ru Login:
w %(user)s
ru Password:
w %(pwd)s
w cd ~/interestingDir
w ls -l
ra
w exit
c
"""</p>
<p>user = 'foo'
pwd = 'bar'
conn = telnetscript.telnetscript( 'myserver', vars() )
lines = conn.RunScript( script.split( '\n' ))</p>
<p>This assigns lines the value of the output of "ls" in "~/interestingDir" for user foo on myserver.</p>
get names of all "up" network interfaces (linux only) (Python)
2005-08-11T13:17:36-07:00paul cannonhttp://code.activestate.com/recipes/users/2551140/http://code.activestate.com/recipes/439093-get-names-of-all-up-network-interfaces-linux-only/
<p style="color: grey">
Python
recipe 439093
by <a href="/recipes/users/2551140/">paul cannon</a>
(<a href="/recipes/tags/network/">network</a>).
</p>
<p>Uses the SIOCGIFCONF ioctl to obtain a list of interfaces and extracts those names, returning them in a list of strings.</p>
stock prices historical data bulk download from internet (Python)
2007-04-01T14:05:47-07:00gian paolo cicerihttp://code.activestate.com/recipes/users/631476/http://code.activestate.com/recipes/511444-stock-prices-historical-data-bulk-download-from-in/
<p style="color: grey">
Python
recipe 511444
by <a href="/recipes/users/631476/">gian paolo ciceri</a>
(<a href="/recipes/tags/network/">network</a>).
</p>
<p>Courtesy of Yahoo finance, it is possible to bulk download historical prices data. This script, borrowed from pycurl retriever-multi.py example, fetch series for several tickers at a time. It uses urllib to fetch web data, so it should work with a plain vanilla python distro.</p>