Top-rated recipes tagged "meta:requires=os"http://code.activestate.com/recipes/tags/meta:requires=os/top/2014-06-30T20:30:57-07:00ActiveState Code RecipesCreating a daemon the Python way (Python)
2005-10-03T16:49:09-07:00Chad J. Schroederhttp://code.activestate.com/recipes/users/1760491/http://code.activestate.com/recipes/278731-creating-a-daemon-the-python-way/
<p style="color: grey">
Python
recipe 278731
by <a href="/recipes/users/1760491/">Chad J. Schroeder</a>
(<a href="/recipes/tags/threads/">threads</a>).
Revision 6.
</p>
<p>The Python way to detach a process from the controlling terminal and run it in the
background as a daemon.</p>
Sorting big files the Python 2.4 way (Python)
2006-04-13T10:43:13-07:00Nicolas Lehuenhttp://code.activestate.com/recipes/users/1599156/http://code.activestate.com/recipes/466302-sorting-big-files-the-python-24-way/
<p style="color: grey">
Python
recipe 466302
by <a href="/recipes/users/1599156/">Nicolas Lehuen</a>
(<a href="/recipes/tags/files/">files</a>).
Revision 6.
</p>
<p>This recipe can be used to sort big files (much bigger than the available RAM) according to a key. The sort is guaranteed to be stable on Python 2.3.</p>
Module to allow Asynchronous subprocess use on Windows and Posix platforms (Python)
2006-12-01T17:30:02-08:00Josiah Carlsonhttp://code.activestate.com/recipes/users/1241800/http://code.activestate.com/recipes/440554-module-to-allow-asynchronous-subprocess-use-on-win/
<p style="color: grey">
Python
recipe 440554
by <a href="/recipes/users/1241800/">Josiah Carlson</a>
(<a href="/recipes/tags/sysadmin/">sysadmin</a>).
Revision 10.
</p>
<p>The 'subprocess' module in Python 2.4 has made creating and accessing subprocess streams in Python relatively convenient for all supported platforms, but what if you want to interact with the started subprocess? That is, what if you want to send a command, read the response, and send a new command based on that response?</p>
<p>Now there is a solution. The included subprocess.Popen subclass adds three new commonly used methods: recv(maxsize=None), recv_err(maxsize=None), and send(input), along with a utility method: send_recv(input='', maxsize=None).</p>
<p>recv() and recv_err() both read at most maxsize bytes from the started subprocess.
send() sends strings to the started subprocess.
send_recv() will send the provided input, and read up to maxsize bytes from both stdout and stderr.</p>
<p>If any of the pipes are closed, the attributes for those pipes will be set to None, and the methods will return None.</p>
<p>v. 1.3 fixed a few bugs relating to *nix support
v. 1.4,5 fixed initialization on all platforms, a few bugs relating to Windows support, added two utility functions, and added an example of how to use this module.
v. 1.6 fixed linux _recv() and test initialization thanks to Yuri Takhteyev at Stanford.
v. 1.7 removed _setup() and __init__() and fixed subprocess unittests thanks to Antonio Valentino. Added 4th argument 'tr' to recv_some(), which is, approximately, the number of times it will attempt to recieve data. Added 5th argument 'stderr' to recv_some(), where when true, will recieve from stderr. Cleaned up some pipe closing.
v. 1.8 Fixed missing self. parameter in non-windows _recv method thanks to comment.
v. 1.9 Fixed fcntl calls for closed handles.</p>
Fork a daemon process on Unix (Python)
2001-07-10T14:01:38-07:00Jürgen Hermannhttp://code.activestate.com/recipes/users/98061/http://code.activestate.com/recipes/66012-fork-a-daemon-process-on-unix/
<p style="color: grey">
Python
recipe 66012
by <a href="/recipes/users/98061/">Jürgen Hermann</a>
(<a href="/recipes/tags/threads/">threads</a>).
</p>
<p>Forking a daemon on Unix requires a certain sequence of system calls. Since Python exposes a full POSIX interface, this can be done in Python, too.</p>
Persistent dict with multiple standard file formats (Python)
2011-09-06T20:01:46-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/576642-persistent-dict-with-multiple-standard-file-format/
<p style="color: grey">
Python
recipe 576642
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/dbm/">dbm</a>, <a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/persistent/">persistent</a>, <a href="/recipes/tags/shelve/">shelve</a>).
Revision 10.
</p>
<p>dbdict: a dbm based on a dict subclass.</p>
<p>On open, loads full file into memory.
On close, writes full dict to disk (atomically).
Supported output file formats: csv, json, and pickle.
Input file format automatically discovered.</p>
<p>Usable by the shelve module for fast access.</p>
PyDbLite, a small in-memory database engine (Python)
2011-07-18T19:36:04-07:00Pierre Quentelhttp://code.activestate.com/recipes/users/1552957/http://code.activestate.com/recipes/496770-pydblite-a-small-in-memory-database-engine/
<p style="color: grey">
Python
recipe 496770
by <a href="/recipes/users/1552957/">Pierre Quentel</a>
(<a href="/recipes/tags/database/">database</a>).
Revision 4.
</p>
<p>A small, fast, in-memory database management program</p>
<p>The database object supports the iterator protocol, so that requests can be expressed with list comprehensions or generator expressions instead of SQL. The equivalent of :</p>
<pre class="prettyprint"><code>cursor.execute("SELECT name FROM table WHERE age=30")
rows = cursor.fetchall()
</code></pre>
<p>is :</p>
<pre class="prettyprint"><code>rows = table(age=30)
</code></pre>
<p>The module stores data in a cPickled file. Records are indexed by a unique record identifier, that can be used for direct access. Since operations are processed in memory they are extremely fast, nearly as fast as SQLite in the few tests I made, and MUCH faster than other pure-Python modules such as Gadfly or KirbyBase. An index can be created on a field to even speed up selections</p>
<p>Concurrency control is supported by a version number set for each record</p>
<p>Complete documentation is <a href="http://www.pydblite.net">here</a></p>
Simple HTTP server supporting SSL secure communications (Python)
2008-08-02T16:04:56-07:00Sebastien Martinihttp://code.activestate.com/recipes/users/2637141/http://code.activestate.com/recipes/442473-simple-http-server-supporting-ssl-secure-communica/
<p style="color: grey">
Python
recipe 442473
by <a href="/recipes/users/2637141/">Sebastien Martini</a>
(<a href="/recipes/tags/https/">https</a>, <a href="/recipes/tags/openssl/">openssl</a>, <a href="/recipes/tags/ssl/">ssl</a>, <a href="/recipes/tags/web/">web</a>).
Revision 8.
</p>
<p>This recipe describes how to set up a simple HTTP server supporting SSL secure communications. It extends the SimpleHTTPServer standard module to support the SSL protocol. With this recipe, only the server is authenticated while the client remains unauthenticated (i.e. the server will not request a client certificate). Thus, the client (typically the browser) will be able to verify the server identity and secure its communications with the server.</p>
<p>This recipe requires you already know the basis of SSL and how to set up <a href="http://www.openssl.org">OpenSSL</a>. This recipe is mostly derived from the examples provided with the <a href="http://pyopenssl.sourceforge.net">pyOpenSSL</a> package.</p>
<h5>In order to apply this recipe, follow these few steps:</h5>
<ol>
<li>Install the OpenSSL package in order to generate key and certificate. Note: you probably already have this package installed if you are under Linux, or *BSD.</li>
<li>Install the pyOpenSSL package, it is an OpenSSL library binding. You'll need to import this module for accessing OpenSSL's components.</li>
<li>Generate a self-signed certificate compounded of a certificate and a private key for your server with the following command (it outputs them both in a single file named server.pem):
<code>openssl req -new -x509 -keyout server.pem -out server.pem -days 365 -nodes</code></li>
<li>Assuming you saved this recipe in SimpleSecureHTTPServer.py, start the server (with the appropriate rights):
<code>python SimpleSecureHTTPServer.py</code></li>
<li>Finally, browse to <a href="https://localhost">https://localhost</a>, or <a href="https://localhost:port" rel="nofollow">https://localhost:port</a> if your server listens a different port than 443.</li>
</ol>
firefox sqlite files cleaner (Python)
2009-07-18T21:41:13-07:00peekaahttp://code.activestate.com/recipes/users/2919471/http://code.activestate.com/recipes/576842-firefox-sqlite-files-cleaner/
<p style="color: grey">
Python
recipe 576842
by <a href="/recipes/users/2919471/">peekaa</a>
(<a href="/recipes/tags/firefox/">firefox</a>, <a href="/recipes/tags/sqlite/">sqlite</a>).
Revision 7.
</p>
<p>Walks through the all the Firefox profiles in current user account and cleans all
*.sqlite files with "vacuum". It makes firefox faster then often. Should work on Linux, too, when properly changed constants.</p>
<p>For: python 2.6 (2.5+sqlite3), FF 3.5</p>
Povray for python (Python)
2003-06-13T21:22:18-07:00Simon Burtonhttp://code.activestate.com/recipes/users/860809/http://code.activestate.com/recipes/205451-povray-for-python/
<p style="color: grey">
Python
recipe 205451
by <a href="/recipes/users/860809/">Simon Burton</a>
(<a href="/recipes/tags/graphics/">graphics</a>).
</p>
<p>Here is a relatively simple framework for making povray files from your favourite programming language, python. It's good for creating structured/mathematical scenes and animations.</p>
Finite State Machine (FSM) (Python)
2007-12-05T01:25:49-08:00Noah Spurrierhttp://code.activestate.com/recipes/users/103276/http://code.activestate.com/recipes/146262-finite-state-machine-fsm/
<p style="color: grey">
Python
recipe 146262
by <a href="/recipes/users/103276/">Noah Spurrier</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 2.
</p>
<p>This recipe shows a Finite State Machine (FSM) that can be used for small parsing tasks. The code is quite simple. The bulk of it is comments. In addition to state this FSM also maintains a user defined "memory". So this FSM is a Push-down Automata (PDA) since a PDA is a FSM + memory. This module contains an example function that demonstrates a simple RPN expression evaluator.</p>
Send HTML or Text email with or without attachments (Python)
2009-07-27T13:14:24-07:00Isaac Wagnerhttp://code.activestate.com/recipes/users/4171255/http://code.activestate.com/recipes/576858-send-html-or-text-email-with-or-without-attachment/
<p style="color: grey">
Python
recipe 576858
by <a href="/recipes/users/4171255/">Isaac Wagner</a>
(<a href="/recipes/tags/email/">email</a>, <a href="/recipes/tags/smtp/">smtp</a>).
</p>
<p>Using code gleaned from the net and from my own brain I created this convenient wrapper to send email messages via SMTP in Python. This class allows you to send plain text email messages or HTML encoded messages. You can also add attachments to the messages.</p>
PathCatcher - Windows utility for right-click capture of file or folder path (Python)
2008-04-19T17:14:18-07:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/528893-pathcatcher-windows-utility-for-right-click-captur/
<p style="color: grey">
Python
recipe 528893
by <a href="/recipes/users/4076953/">Jack Trainor</a>
(<a href="/recipes/tags/shortcuts/">shortcuts</a>).
Revision 3.
</p>
<p>PathCatcher is a Windows utility that allows one to right-click on a file or a folder or a group of files and folders in Explorer and save its path to the clipboard.</p>
<p>To install, save the code as PathCatcher.py, then double-click the PathCatcher.py file. Afterwards, PathCatcher will appear in the Explorer right-click menu.</p>
My first application server (Python)
2009-02-23T11:53:57-08:00Pierre Quentelhttp://code.activestate.com/recipes/users/1552957/http://code.activestate.com/recipes/392879-my-first-application-server/
<p style="color: grey">
Python
recipe 392879
by <a href="/recipes/users/1552957/">Pierre Quentel</a>
(<a href="/recipes/tags/web/">web</a>).
Revision 8.
</p>
<p>For those who want to start dynamic web programming, but don't know what to choose among the many Python web frameworks, this program might be a good starting point</p>
<p>ScriptServer is a minimalist application server, handling both GET and POST requests, including multipart/form-data for file uploads, HTTP redirections, and with an in-memory session management. It can run Python scripts and template files using the standard string substitution format</p>
<p>The scripts are run in the same process as the server, avoiding the CGI overhead. The environment variables are provided in the namespace where the script runs</p>
<p>To start the server, run </p>
<pre class="prettyprint"><code>python ScriptServer.py
</code></pre>
<p>In your web browser, enter <a href="http://localhost" rel="nofollow">http://localhost</a>, this will show you a listing of the directory. Add the scripts in the same directory as ScriptServer</p>
Simple HTTP server based on asyncore/asynchat (Python)
2005-10-16T08:26:59-07:00Pierre Quentelhttp://code.activestate.com/recipes/users/1552957/http://code.activestate.com/recipes/259148-simple-http-server-based-on-asyncoreasynchat/
<p style="color: grey">
Python
recipe 259148
by <a href="/recipes/users/1552957/">Pierre Quentel</a>
(<a href="/recipes/tags/web/">web</a>).
Revision 8.
</p>
<p>A simple HTTP Server, intended to be as simple as the standard module SimpleHTTPServer, built upon the asyncore/asynchat modules (uses non-blocking sockets). Provides a Server (copied from medusa http_server) and a RequestHandler class. RequestHandler handles both GET and POST methods and inherits SimpleHTTPServer.SimpleHTTPRequestHandler</p>
<p>It can be easily extended by overriding the handle_data() method in the RequestHandler class</p>
Non-blocking readlines() (Python)
2014-06-30T20:30:57-07:00Zack Weinberghttp://code.activestate.com/recipes/users/4190298/http://code.activestate.com/recipes/578900-non-blocking-readlines/
<p style="color: grey">
Python
recipe 578900
by <a href="/recipes/users/4190298/">Zack Weinberg</a>
(<a href="/recipes/tags/concurrency/">concurrency</a>, <a href="/recipes/tags/input/">input</a>, <a href="/recipes/tags/line/">line</a>, <a href="/recipes/tags/nonblocking/">nonblocking</a>).
</p>
<p>A generator function which takes a file object (assumed to be some sort of pipe or socket, open for reading), and yields lines from it without blocking. If there is no input available, it will yield an endless stream of empty strings until input becomes available again; caller is responsible for not going into a busy loop. (Newlines are normalized but not stripped, so if there is actually a blank line in the input, the value yielded will be <code>'\n'</code>.) The intended use case is a thread which must respond promptly to input from a pipe, and also something else which cannot be fed to <code>select</code> (e.g. a <code>queue.Queue</code>). Note that the file object is ignored except for its <code>fileno</code>.</p>
<p>Only tested on Unix. Only tested on 3.4; ought to work with any python that has <code>bytearray</code>, <code>locale.getpreferredencoding</code>, and <code>fcntl</code>.</p>
System Authentication against /etc/shadow or /etc/passwd (Python)
2013-03-11T12:40:10-07:00James Millshttp://code.activestate.com/recipes/users/4167757/http://code.activestate.com/recipes/578489-system-authentication-against-etcshadow-or-etcpass/
<p style="color: grey">
Python
recipe 578489
by <a href="/recipes/users/4167757/">James Mills</a>
(<a href="/recipes/tags/authentication/">authentication</a>, <a href="/recipes/tags/password/">password</a>, <a href="/recipes/tags/system/">system</a>).
</p>
<p>Sometimes it's useful to perform System Authentication against a Local System using the /etc/shadow or /etc/passwd password databases. This recipe provides a simple function that does exactly that.</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>
For AMIGA-Heads Only. Poking A HW, (Memory), Address On A Classic AMIGA, Inside Text Mode Python. (Python)
2012-09-09T19:25:00-07:00Barry Walkerhttp://code.activestate.com/recipes/users/4177147/http://code.activestate.com/recipes/578255-for-amiga-heads-only-poking-a-hw-memory-address-on/
<p style="color: grey">
Python
recipe 578255
by <a href="/recipes/users/4177147/">Barry Walker</a>
(<a href="/recipes/tags/amiga/">amiga</a>, <a href="/recipes/tags/demo/">demo</a>, <a href="/recipes/tags/e_uae/">e_uae</a>, <a href="/recipes/tags/hardware/">hardware</a>, <a href="/recipes/tags/memory/">memory</a>, <a href="/recipes/tags/poke/">poke</a>, <a href="/recipes/tags/winuae/">winuae</a>).
</p>
<p>For AMIGA-Heads Only...</p>
<p>This code is purely for a minimum of a stock Classic AMIGA A1200(HD), E-UAE, and WinUAE running AMIGA OS3.0x to 3.1x.
It will NOT work on any other platform!</p>
<p>(It MIGHT work on AMIGA OS3.5x and 3.9x with Python 2.4.6, but totally untested and I hold no responsibility for the outcome when trying it.)</p>
<p>It is a DEMO to poke a byte inside _ANY_ memory address inside the standard CPU 68EC020 16MB boundary.</p>
<p>This is not a function nor a class but just a DEMO to show how to write directly to HW, (or memory), addresses.</p>
<p>POKING memory or hardware addresses is not recommended for novices without studying the machine first, and, this code will ignore any _ENFORCER_hits_.</p>
<p>I experimented with this several years ago and have decided to finally release it. I do await the flak!</p>
<p>It works from Python Versions 1.4.0 to 2.0.1 for the classic AMIGA.</p>
<p>This is issued as Public Domain and you may do with it as you please.</p>
<p>Ensure that the T: Volume exists on the running machine.</p>
<p>See inside the code and text below it also as to how it works...</p>
<p>Bazza, G0LCU...</p>
Execute remote commands on windows like psexec (Python)
2011-11-18T11:54:42-08:00Ofer Helmanhttp://code.activestate.com/recipes/users/4179914/http://code.activestate.com/recipes/577945-execute-remote-commands-on-windows-like-psexec/
<p style="color: grey">
Python
recipe 577945
by <a href="/recipes/users/4179914/">Ofer Helman</a>
(<a href="/recipes/tags/command/">command</a>, <a href="/recipes/tags/remote/">remote</a>, <a href="/recipes/tags/windows/">windows</a>).
Revision 4.
</p>
<p>This code attempts to implement psexec in python code, using wmi.
As part of a project of mine I had to run remote commands on remote Windows machines from other Windows machine. At first I used psexec for that with subprocess.Popen.
The reason in this code for creating .bat files and running them remotely is because complicated commands do not run properly with Win32_Process.Create</p>
<p>In this code I used this code: <a href="http://code.activestate.com/recipes/442521/history/3/" rel="nofollow">http://code.activestate.com/recipes/442521/history/3/</a></p>
<p>required installations:</p>
<p>pywin32 - <a href="http://sourceforge.net/projects/pywin32/files/pywin32/Build216/" rel="nofollow">http://sourceforge.net/projects/pywin32/files/pywin32/Build216/</a></p>
<p>wmi - <a href="http://timgolden.me.uk/python/downloads/" rel="nofollow">http://timgolden.me.uk/python/downloads/</a></p>
Asynchronous subprocess using asyncore (Python)
2013-01-21T19:51:00-08:00Glenn Eychanerhttp://code.activestate.com/recipes/users/4172294/http://code.activestate.com/recipes/576957-asynchronous-subprocess-using-asyncore/
<p style="color: grey">
Python
recipe 576957
by <a href="/recipes/users/4172294/">Glenn Eychaner</a>
(<a href="/recipes/tags/async/">async</a>, <a href="/recipes/tags/asynchronous/">asynchronous</a>, <a href="/recipes/tags/asyncore/">asyncore</a>, <a href="/recipes/tags/coroutine/">coroutine</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/ipc/">ipc</a>, <a href="/recipes/tags/subprocess/">subprocess</a>).
Revision 21.
</p>
<p>A coroutine-based wrapper for subprocess.Popen that uses asyncore to communicate with child processes asynchronously. This allows subprocesses to be called from within socket servers or clients without needing a complicated event loop to check both. Uses <a href="http://code.activestate.com/recipes/576965/">recipe 576965</a> to provide the asynchronous coroutine framework, <a href="http://code.activestate.com/recipes/576967/">recipe 576967</a> to provide asynchronous pipes, and <a href="http://code.activestate.com/recipes/577600/">recipe 577600</a> to provide multiple alarms.</p>