Popular recipes tagged "meta:requires=socket"http://code.activestate.com/recipes/tags/meta:requires=socket/2016-07-03T21:58:49-07:00ActiveState Code RecipesHopfield Artificial Neural Network (C++)
2016-07-03T21:58:49-07:00Jay ballerhttp://code.activestate.com/recipes/users/4194368/http://code.activestate.com/recipes/580688-hopfield-artificial-neural-network/
<p style="color: grey">
C++
recipe 580688
by <a href="/recipes/users/4194368/">Jay baller</a>
(<a href="/recipes/tags/ai/">ai</a>, <a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/artificial_intelligence/">artificial_intelligence</a>, <a href="/recipes/tags/neural_network/">neural_network</a>).
</p>
<p>(Discrete (Binary)) Hopfield Artificial Neural Network (ANN).</p>
<p>For more info see:</p>
<p><a href="http://en.wikipedia.org/wiki/Hopfield_net" rel="nofollow">http://en.wikipedia.org/wiki/Hopfield_net</a></p>
<p><a href="http://www.scholarpedia.org/article/Hopfield_network" rel="nofollow">http://www.scholarpedia.org/article/Hopfield_network</a></p>
Wait for network service to appear (Python)
2014-11-06T07:29:12-08:00Mohammad Taha Jahangirhttp://code.activestate.com/recipes/users/4188847/http://code.activestate.com/recipes/578955-wait-for-network-service-to-appear/
<p style="color: grey">
Python
recipe 578955
by <a href="/recipes/users/4188847/">Mohammad Taha Jahangir</a>
(<a href="/recipes/tags/network/">network</a>, <a href="/recipes/tags/socket/">socket</a>).
</p>
<p>This script allows you to wait until specified port is opened on remote server. This can be useful in automation jobs - restarting server, wake on lan etc. It can also be used for monitoring distant service/site.</p>
<p>The main problem that this script solves is that you need to handle two different timeouts when opening probing socket, and it is not described in python documentation. See <a href="http://bugs.python.org/issue5293" rel="nofollow">http://bugs.python.org/issue5293</a> for more information.</p>
Primitive Peer to Peer Chat (Python)
2013-07-07T02:09:57-07:00teddy_khttp://code.activestate.com/recipes/users/4187115/http://code.activestate.com/recipes/578591-primitive-peer-to-peer-chat/
<p style="color: grey">
Python
recipe 578591
by <a href="/recipes/users/4187115/">teddy_k</a>
(<a href="/recipes/tags/chat/">chat</a>, <a href="/recipes/tags/peer/">peer</a>, <a href="/recipes/tags/select/">select</a>, <a href="/recipes/tags/socket/">socket</a>, <a href="/recipes/tags/threading/">threading</a>).
</p>
<p>This took me longer than it should have. I am putting it here in the hopes that this post will spare others from having to Google things for extended periods of time.</p>
<p>In short, you either listen for a connection (Chat_Server), or connect to a remote IP address (Chat_Client). From there, you can send text strings back and forth. </p>
<p>This is a bit rough-hewn, obviously; I apologize in advance.</p>
Sending Email in Python (Python)
2013-02-24T13:47:01-08:00James Millshttp://code.activestate.com/recipes/users/4167757/http://code.activestate.com/recipes/578472-sending-email-in-python/
<p style="color: grey">
Python
recipe 578472
by <a href="/recipes/users/4167757/">James Mills</a>
(<a href="/recipes/tags/email/">email</a>, <a href="/recipes/tags/email_sending/">email_sending</a>, <a href="/recipes/tags/sendmail/">sendmail</a>, <a href="/recipes/tags/smtp/">smtp</a>).
</p>
<p>Every Python Application needs to send email at some point. Whether it's for reporting errors, status updates or simply the core functionality of the system this little recipe should help! Documented and Tested.</p>
Read text data from the network port (Python)
2012-12-04T20:17:59-08:00anatoly techtonikhttp://code.activestate.com/recipes/users/4168147/http://code.activestate.com/recipes/578355-read-text-data-from-the-network-port/
<p style="color: grey">
Python
recipe 578355
by <a href="/recipes/users/4168147/">anatoly techtonik</a>
(<a href="/recipes/tags/network/">network</a>).
</p>
<p>Here is a way to wait for incoming text on some port and print it to the screen. This is the best technique I could come up with.</p>
WebSocket interface (Python)
2012-11-25T16:52:21-08:00Nick Farohttp://code.activestate.com/recipes/users/4184363/http://code.activestate.com/recipes/578348-websocket-interface/
<p style="color: grey">
Python
recipe 578348
by <a href="/recipes/users/4184363/">Nick Faro</a>
(<a href="/recipes/tags/javascript/">javascript</a>, <a href="/recipes/tags/network/">network</a>, <a href="/recipes/tags/socket/">socket</a>, <a href="/recipes/tags/websocket/">websocket</a>).
Revision 2.
</p>
<p>This tries its best to be a replacement for the regular <code>socket</code> module.</p>
<p>It supports only sending and receiving but should be useful enough.</p>
<p>The only real difference should be that you can't specify the number of bytes is received, instead do</p>
<pre class="prettyprint"><code>for message in socket.recv():
print(message)
</code></pre>
<p>Revision 2:
Added proper message receiving. Previously it just requested a ton of data. Now it reads 2 bytes, determines the length, then requests that much.</p>
OS2::UPM (undef) (Perl)
2012-09-30T19:25:20-07:00Roger Mbiama Assogohttp://code.activestate.com/recipes/users/4182949/http://code.activestate.com/recipes/578273-os2upm-undef/
<p style="color: grey">
Perl
recipe 578273
by <a href="/recipes/users/4182949/">Roger Mbiama Assogo</a>
(<a href="/recipes/tags/bug/">bug</a>, <a href="/recipes/tags/reportlab/">reportlab</a>).
</p>
<p>The CPAN Testers Family: Os: linux apache(kernel)</p>
Calendar Maker (Python)
2012-07-03T05:23:57-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/578187-calendar-maker/
<p style="color: grey">
Python
recipe 578187
by <a href="/recipes/users/2608421/">Stephen Chappell</a>
(<a href="/recipes/tags/archive/">archive</a>, <a href="/recipes/tags/calendar/">calendar</a>, <a href="/recipes/tags/cgi/">cgi</a>, <a href="/recipes/tags/old/">old</a>).
</p>
<p>This is an old program experimenting with CGI techniques in Python using <code>CGIHTTPServer</code> and designed for creating calendars. This is committed for archival to be run under Python 2.5 or later versions.</p>
Distributed lock manager for Python (Python)
2012-07-04T21:03:32-07:00pavelhttp://code.activestate.com/recipes/users/4171837/http://code.activestate.com/recipes/578194-distributed-lock-manager-for-python/
<p style="color: grey">
Python
recipe 578194
by <a href="/recipes/users/4171837/">pavel</a>
(<a href="/recipes/tags/distributed/">distributed</a>, <a href="/recipes/tags/lock/">lock</a>, <a href="/recipes/tags/locking/">locking</a>, <a href="/recipes/tags/locks/">locks</a>, <a href="/recipes/tags/manager/">manager</a>, <a href="/recipes/tags/socket/">socket</a>, <a href="/recipes/tags/thread/">thread</a>, <a href="/recipes/tags/threading/">threading</a>, <a href="/recipes/tags/wsgi/">wsgi</a>).
Revision 2.
</p>
<p>Distributed lock manager provides mutex(es) over network. It is used to synchronize processes running on different machines, e.g. WSGI processes in case of web applications. Lock object is compatible with threading.Lock and can be used as a context manager ("with statement"). It can be easily modified to use UNIX sockets instead of TCP/IP. Communication protocol is text based.</p>
<p>First start server process:</p>
<pre class="prettyprint"><code>$ chmod +x dlm.py
$ ./dlm.py
</code></pre>
<p>Usage:</p>
<pre class="prettyprint"><code>from dlm import LockClient
client = LockClient('localhost', 27272, 'client_name')
lock = client.mkLock('lock_name')
lock.acquire()
# critical section here...
lock.release()
# using context manager
with lock:
# critical section here...
</code></pre>
Multithreading Downloader Class (Python)
2012-07-22T07:44:20-07:00Itay Brandeshttp://code.activestate.com/recipes/users/4182927/http://code.activestate.com/recipes/578220-multithreading-downloader-class/
<p style="color: grey">
Python
recipe 578220
by <a href="/recipes/users/4182927/">Itay Brandes</a>
(<a href="/recipes/tags/downloader/">downloader</a>, <a href="/recipes/tags/multithread/">multithread</a>, <a href="/recipes/tags/multithreading/">multithreading</a>, <a href="/recipes/tags/urllib/">urllib</a>, <a href="/recipes/tags/urllib2/">urllib2</a>, <a href="/recipes/tags/urlopen/">urlopen</a>).
</p>
<p>Garbs files from the web using multithreading in an attempt to enhance transfer rate.</p>
Set windows 7 to lock itself (upon timeout) if no internet connection found - security measure (Python)
2012-07-09T20:56:22-07:00commentator8http://code.activestate.com/recipes/users/4182761/http://code.activestate.com/recipes/578200-set-windows-7-to-lock-itself-upon-timeout-if-no-in/
<p style="color: grey">
Python
recipe 578200
by <a href="/recipes/users/4182761/">commentator8</a>
(<a href="/recipes/tags/lock/">lock</a>, <a href="/recipes/tags/password/">password</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/windows/">windows</a>, <a href="/recipes/tags/windows_registry/">windows_registry</a>).
Revision 2.
</p>
<p>This script (or exe if using web2py with """setup(windows=['locker2.py']))""") can be run as a task in windows every x minutes and will test for the presence of an internet connection and depending on whether it is found will set windows to lock after a given timeout without user activity.</p>
<p>This was made with help from random code snippets from around the web.</p>
<p>Tested only on windows 7.</p>
A Simple Webcrawler (Python)
2012-03-03T02:37:30-08:00Johnhttp://code.activestate.com/recipes/users/4181142/http://code.activestate.com/recipes/578060-a-simple-webcrawler/
<p style="color: grey">
Python
recipe 578060
by <a href="/recipes/users/4181142/">John</a>
(<a href="/recipes/tags/crawler/">crawler</a>, <a href="/recipes/tags/html/">html</a>, <a href="/recipes/tags/page/">page</a>, <a href="/recipes/tags/parser/">parser</a>, <a href="/recipes/tags/scraping/">scraping</a>, <a href="/recipes/tags/urllib/">urllib</a>, <a href="/recipes/tags/urlopen/">urlopen</a>, <a href="/recipes/tags/web/">web</a>).
</p>
<p>This is my simple web crawler. It takes as input a list of seed pages (web urls) and 'scrapes' each page of all its absolute path links (i.e. links in the format <a href="http://" rel="nofollow">http://</a>) and adds those to a dictionary. The web crawler can take all the links found in the seed pages and then scrape those as well. You can continue scraping as deep as you like. You can control how "deep you go" by specifying the depth variable passed into the WebCrawler class function start_crawling(seed_pages,depth). Think of the depth as the recursion depth (or the number of web pages deep you go before returning back up the tree).</p>
<p>To make this web crawler a little more interesting I added some bells and whistles. I added the ability to pass into the WebCrawler class constructor a regular expression object. The regular expression object is used to "filter" the links found during scraping. For example, in the code below you will see:</p>
<p>cnn_url_regex = re.compile('(?<=[.]cnn)[.]com') # cnn_url_regex is a regular expression object</p>
<p>w = WebCrawler(cnn_url_regex)</p>
<p>This particular regular expression says:</p>
<p>1) Find the first occurence of the string '.com'</p>
<p>2) Then looking backwards from where '.com' was found it attempts to find '.cnn'</p>
<p>Why do this?</p>
<p>You can control where the crawler crawls. In this case I am constraining the crawler to operate on webpages within cnn.com.</p>
<p>Another feature I added was the ability to parse a given page looking for specific html tags. I chose as an example the <h1> tag. Once a <h1> tag is found I store all the words I find in the tag in a dictionary that gets associated with the page url.</p>
<p>Why do this?</p>
<p>My thought was that if I scraped the page for text I could eventually use this data for a search engine request. Say I searched for 'Lebron James'. And suppose that one of the pages my crawler scraped found an article that mentions Lebron James many times. In response to a search request I could return the link with the Lebron James article in it.</p>
<p>The web crawler is described in the WebCrawler class. It has 2 functions the user should call:</p>
<p>1) start_crawling(seed_pages,depth)</p>
<p>2) print_all_page_text() # this is only used for debug purposes</p>
<p>The rest of WebCrawler's functions are internal functions that should not be called by the user (think private in C++).</p>
<p>Upon construction of a WebCrawler object, it creates a MyHTMLParser object. The MyHTMLParser class inherits from the built-in Python class HTMLParser. I use the MyHTMLParser object when searching for the <h1> tag. The MyHTMLParser class creates instances of a helper class named Tag. The tag class is used in creating a "linked list" of tags.</p>
<p>So to get started with WebCrawler make sure to use Python 2.7.2. Enter the code a piece at a time into IDLE in the order displayed below. This ensures that you import libs before you start using them.</p>
<p>Once you have entered all the code into IDLE, you can start crawling the 'interwebs' by entering the following:</p>
<p>import re</p>
<p>cnn_url_regex = re.compile('(?<=[.]cnn)[.]com') </p>
<p>w = WebCrawler(cnn_url_regex)</p>
<p>w.start_crawling(['http://www.cnn.com/2012/02/24/world/americas/haiti-pm-resigns/index.html?hpt=hp_t3'],1)</p>
<p>Of course you can enter any page you want. But the regular expression object is already setup to filter on <a href="http://cnn.com" rel="nofollow">cnn.com</a>. Remember the second parameter passed into the start_crawling function is the recursion depth.</p>
<p>Happy Crawling!</p>
Transparent HTTP Tunnel for Python sockets (to be used by ftplib ) (Python)
2011-11-07T10:25:56-08:00Raphaël Jolivethttp://code.activestate.com/recipes/users/4135673/http://code.activestate.com/recipes/577643-transparent-http-tunnel-for-python-sockets-to-be-u/
<p style="color: grey">
Python
recipe 577643
by <a href="/recipes/users/4135673/">Raphaël Jolivet</a>
(<a href="/recipes/tags/ftp/">ftp</a>, <a href="/recipes/tags/ftplib/">ftplib</a>, <a href="/recipes/tags/http/">http</a>, <a href="/recipes/tags/over/">over</a>, <a href="/recipes/tags/proxy/">proxy</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/socket/">socket</a>, <a href="/recipes/tags/socks/">socks</a>).
Revision 4.
</p>
<p>This script allows how to transparently install a HTTP proxy (proxy HTTP 1.1, using CONNECT command) on all outgoing sockets.</p>
<p>I did that to bring TCP over HTTP to FTPlib, transparently.
It should enable HTTP tunneling for all methods / modules that use the low-level socket API.</p>
Socket Broadcast Help (Python)
2011-12-04T22:18:13-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/577950-socket-broadcast-help/
<p style="color: grey">
Python
recipe 577950
by <a href="/recipes/users/2608421/">Stephen Chappell</a>
(<a href="/recipes/tags/broadcast/">broadcast</a>, <a href="/recipes/tags/socket/">socket</a>, <a href="/recipes/tags/udp/">udp</a>).
Revision 6.
</p>
<p>The classes in this module are stepping stones for building discoverable
services on a network. Server replies are to be handled by the importer.</p>
TCP Port Checker (Python)
2011-06-24T09:53:28-07:00Boubakrhttp://code.activestate.com/recipes/users/4176416/http://code.activestate.com/recipes/577769-tcp-port-checker/
<p style="color: grey">
Python
recipe 577769
by <a href="/recipes/users/4176416/">Boubakr</a>
(<a href="/recipes/tags/checker/">checker</a>, <a href="/recipes/tags/port/">port</a>, <a href="/recipes/tags/tcp/">tcp</a>).
</p>
<p>a simple TCP Port Checker using socket !</p>
HTTPS httplib Client Connection with Certificate Validation (Python)
2011-01-18T18:30:45-08:00Marcelo Fernándezhttp://code.activestate.com/recipes/users/4173551/http://code.activestate.com/recipes/577548-https-httplib-client-connection-with-certificate-v/
<p style="color: grey">
Python
recipe 577548
by <a href="/recipes/users/4173551/">Marcelo Fernández</a>
(<a href="/recipes/tags/certificate/">certificate</a>, <a href="/recipes/tags/client/">client</a>, <a href="/recipes/tags/client_server/">client_server</a>, <a href="/recipes/tags/httplib/">httplib</a>, <a href="/recipes/tags/https/">https</a>, <a href="/recipes/tags/networking/">networking</a>, <a href="/recipes/tags/ssl/">ssl</a>, <a href="/recipes/tags/validation/">validation</a>).
</p>
<p>Despite httplib.HTTPSConnection lets the programmer specify the client's pair of certificates, it doesn't force the underlying SSL library to check the server certificate against the client keys (from the client point of view).</p>
<p>This class allows to force this check, to ensure the python client is connecting to the right server.</p>
Pickle to/from socket (Python)
2011-04-19T22:22:53-07:00pavelhttp://code.activestate.com/recipes/users/4171837/http://code.activestate.com/recipes/577667-pickle-tofrom-socket/
<p style="color: grey">
Python
recipe 577667
by <a href="/recipes/users/4171837/">pavel</a>
(<a href="/recipes/tags/pickle/">pickle</a>, <a href="/recipes/tags/socket/">socket</a>).
Revision 2.
</p>
<p>It's useful for transfering objects through socket, when doing communication between processes or networking.</p>
NBD server in python (Python)
2011-02-08T20:59:23-08:00Dima Tisnekhttp://code.activestate.com/recipes/users/4068698/http://code.activestate.com/recipes/577569-nbd-server-in-python/
<p style="color: grey">
Python
recipe 577569
by <a href="/recipes/users/4068698/">Dima Tisnek</a>
(<a href="/recipes/tags/analyze/">analyze</a>, <a href="/recipes/tags/block/">block</a>, <a href="/recipes/tags/device/">device</a>, <a href="/recipes/tags/disk/">disk</a>, <a href="/recipes/tags/io/">io</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/nbd/">nbd</a>, <a href="/recipes/tags/network/">network</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>Linux Network Block Device server in Python</p>
<p>This is a simplified version based on Kragen Sitaker's <a href="http://lists.canonical.org/pipermail/kragen-hacks/2004-May/000397.html" rel="nofollow">http://lists.canonical.org/pipermail/kragen-hacks/2004-May/000397.html</a></p>
<p>Close is never actually called, at least not on the same connection -- linux C nbd-client -d seems to stall, perhaps it tries to open another socket?</p>
<p>This code doesn't check for error conditions, failed reads/writes, past end of disk, etc.</p>
<p>It prints io requests, you can analyze filesystem and user program io patterns.</p>
whois client (Python)
2010-09-15T12:08:34-07:00Chris Wolfhttp://code.activestate.com/recipes/users/4173108/http://code.activestate.com/recipes/577364-whois-client/
<p style="color: grey">
Python
recipe 577364
by <a href="/recipes/users/4173108/">Chris Wolf</a>
(<a href="/recipes/tags/nic/">nic</a>, <a href="/recipes/tags/whois/">whois</a>).
Revision 6.
</p>
<p>Python whois utility which looks up records in the databases maintained by severa Network Information Centers (NICs). Functions exactly like the UNIX whois(1) utility.</p>
<p>Callable from the command-line, but also has an function entry point for calling from code.</p>
Simplest example of serving a browser request (Python)
2010-10-15T21:17:30-07:00Kaushik Ghosehttp://code.activestate.com/recipes/users/4166965/http://code.activestate.com/recipes/577428-simplest-example-of-serving-a-browser-request/
<p style="color: grey">
Python
recipe 577428
by <a href="/recipes/users/4166965/">Kaushik Ghose</a>
(<a href="/recipes/tags/network/">network</a>, <a href="/recipes/tags/webserver/">webserver</a>).
</p>
<p>This is the simplest way I have found of sending information from a python program to a browser</p>