Most viewed recipes tagged "meta:requires=os"http://code.activestate.com/recipes/tags/meta:requires=os/views/2017-07-22T15:52:01-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> Uno (Text-Based) (Python) 2017-07-15T00:46:59-07:00Brandon Martinhttp://code.activestate.com/recipes/users/4194238/http://code.activestate.com/recipes/580811-uno-text-based/ <p style="color: grey"> Python recipe 580811 by <a href="/recipes/users/4194238/">Brandon Martin</a> (<a href="/recipes/tags/artificial_intelligence/">artificial_intelligence</a>, <a href="/recipes/tags/cards/">cards</a>, <a href="/recipes/tags/game/">game</a>, <a href="/recipes/tags/text_game/">text_game</a>, <a href="/recipes/tags/uno/">uno</a>). </p> <p>A text based recreation of the classic card game featuring functional AIs to play with. Some rules have been modified. User interface is text based, non-curses, using only simple python commands to draw it. </p> Get the path of the currently executing python script using import. (Python) 2006-02-24T05:23:36-08:00Jitender Cheemahttp://code.activestate.com/recipes/users/2790517/http://code.activestate.com/recipes/474083-get-the-path-of-the-currently-executing-python-scr/ <p style="color: grey"> Python recipe 474083 by <a href="/recipes/users/2790517/">Jitender Cheema</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). </p> <h4>1. sys.arg has the script name</h4> <h4>2. Although there can be many ways, e.g. os.cwd() but, there is another</h4> <h4>trick to</h4> <h4>obtain the Complete URI or Location of the current script.</h4> <h4>You can argue, os.getcwd()</h4> <h4>import can give you working directory of the current script</h4> <h4>Third party Java code calling your script.... ;)</h4> <p>#</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> 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> Find a file given a search path (Python) 2001-03-09T23:48:58-08:00Chui Teyhttp://code.activestate.com/recipes/users/98139/http://code.activestate.com/recipes/52224-find-a-file-given-a-search-path/ <p style="color: grey"> Python recipe 52224 by <a href="/recipes/users/98139/">Chui Tey</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). </p> <p>Given search paths separated by a separator, find the first file that matches a given specification.</p> Downloading a File from the Web (Python) 2007-06-13T09:15:37-07:00Aristotelis Mikropouloshttp://code.activestate.com/recipes/users/2881737/http://code.activestate.com/recipes/496685-downloading-a-file-from-the-web/ <p style="color: grey"> Python recipe 496685 by <a href="/recipes/users/2881737/">Aristotelis Mikropoulos</a> (<a href="/recipes/tags/ui/">ui</a>). </p> <p>This script reads the contents of a web file and copies them into a local file, named the same as the web file.</p> Search and replace text in a file. (Python) 2001-03-13T20:18:13-08:00Jeff Bauerhttp://code.activestate.com/recipes/users/98055/http://code.activestate.com/recipes/52250-search-and-replace-text-in-a-file/ <p style="color: grey"> Python recipe 52250 by <a href="/recipes/users/98055/">Jeff Bauer</a> (<a href="/recipes/tags/text/">text</a>). </p> <p>A simple text search and replace program.</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> Minimal http upload cgi (Python) 2004-03-20T01:47:04-08:00Noah Spurrierhttp://code.activestate.com/recipes/users/103276/http://code.activestate.com/recipes/273844-minimal-http-upload-cgi/ <p style="color: grey"> Python recipe 273844 by <a href="/recipes/users/103276/">Noah Spurrier</a> (<a href="/recipes/tags/web/">web</a>). Revision 3. </p> <p>This is a bare-bones cgi file upload. It will display an upload form and save the uploaded files to disk.</p> PIL and Tkinter to display images. (Python) 2007-06-20T19:25:41-07:00Noah Spurrierhttp://code.activestate.com/recipes/users/103276/http://code.activestate.com/recipes/521918-pil-and-tkinter-to-display-images/ <p style="color: grey"> Python recipe 521918 by <a href="/recipes/users/103276/">Noah Spurrier</a> (<a href="/recipes/tags/graphics/">graphics</a>). Revision 3. </p> <p>Using PIL and Tkinter you can easily display images in a window.</p> Locating files throughout a directory tree (Python) 2009-12-02T07:30:27-08:00Simon Brunninghttp://code.activestate.com/recipes/users/98010/http://code.activestate.com/recipes/499305-locating-files-throughout-a-directory-tree/ <p style="color: grey"> Python recipe 499305 by <a href="/recipes/users/98010/">Simon Brunning</a> (<a href="/recipes/tags/files/">files</a>). Revision 3. </p> <p>os.walk is a very nice replacement for os.path.walk, which I never did feel comfortable with. There's one very common pattern of usage, though, which still benefits from a simple helper-function; locating all files matching a given file-name pattern within a directory tree.</p> Find and replace string in all files in a directory (Python) 2004-04-12T05:56:11-07:00Anthony Barkerhttp://code.activestate.com/recipes/users/122940/http://code.activestate.com/recipes/277753-find-and-replace-string-in-all-files-in-a-director/ <p style="color: grey"> Python recipe 277753 by <a href="/recipes/users/122940/">Anthony Barker</a> (<a href="/recipes/tags/text/">text</a>). </p> <p>A friend of mine needed to change the IP address in his DNS hosting bind configuration. Here is a quick script I wrote during lunch it interates all files in a directory and finds and replaces as string. In this case an IP address.</p> Dupinator -- detect and delete duplicate files (Python) 2005-01-09T12:31:21-08:00Bill Bumgarnerhttp://code.activestate.com/recipes/users/2250923/http://code.activestate.com/recipes/362459-dupinator-detect-and-delete-duplicate-files/ <p style="color: grey"> Python recipe 362459 by <a href="/recipes/users/2250923/">Bill Bumgarner</a> (<a href="/recipes/tags/files/">files</a>). </p> <p>Point this script at a folder or several folders and it will find and delete all duplicate files within the folders, leaving behind the first file found of any set of duplicates. It is designed to handle hundreds of thousands of files of any size at a time and to do so quickly. It was written to eliminate duplicates across several photo libraries that had been shared between users. As the script was a one-off to solve a very particular problem, there are no options nor is it refactoring into any kind of modules or reusable functions.</p> A very simple session handling example (Python) 2004-10-28T10:38:30-07:00Jonas Galvezhttp://code.activestate.com/recipes/users/2122857/http://code.activestate.com/recipes/325484-a-very-simple-session-handling-example/ <p style="color: grey"> Python recipe 325484 by <a href="/recipes/users/2122857/">Jonas Galvez</a> (<a href="/recipes/tags/cgi/">cgi</a>). Revision 4. </p> <p>This is a very simple session handling example that uses plain Python CGI (tested only under Python 2.2+). Its goal is to show how cookies are set via HTTP and how easily they can be used for session management.</p> Simple audio with ctypes and SDL also for Tkinter (Python) 2015-08-17T18:54:17-07:00Jiri Justrahttp://code.activestate.com/recipes/users/4192188/http://code.activestate.com/recipes/579070-simple-audio-with-ctypes-and-sdl-also-for-tkinter/ <p style="color: grey"> Python recipe 579070 by <a href="/recipes/users/4192188/">Jiri Justra</a> (<a href="/recipes/tags/audio/">audio</a>, <a href="/recipes/tags/sdl/">sdl</a>, <a href="/recipes/tags/sdl_mixer/">sdl_mixer</a>, <a href="/recipes/tags/tkinter/">tkinter</a>). Revision 3. </p> <p>I've needed just to play audio in my Tkinter application, but it seems there is no easy way to do this, so I have made this simple code. It is small ctypes wrapper around SDL_mixer.</p> <p>It should work for Win and *nix also .. I've tested it on ubuntu-14.04.3-desktop-i386</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> List classes, methods and functions in a module (Python) 2008-10-22T08:15:26-07:00Anandhttp://code.activestate.com/recipes/users/760763/http://code.activestate.com/recipes/553262-list-classes-methods-and-functions-in-a-module/ <p style="color: grey"> Python recipe 553262 by <a href="/recipes/users/760763/">Anand</a> (<a href="/recipes/tags/programs/">programs</a>). Revision 5. </p> <p>The recipe provides a method "describe" which takes a module as argument and describes classes, methods and functions in the module. The method/function description provides information on the function/method arguments using the inspect module.</p> Size of Python objects (revised). (Python) 2017-07-22T15:52:01-07:00Jean Brouwershttp://code.activestate.com/recipes/users/2984142/http://code.activestate.com/recipes/546530-size-of-python-objects-revised/ <p style="color: grey"> Python recipe 546530 by <a href="/recipes/users/2984142/">Jean Brouwers</a> (<a href="/recipes/tags/getsizeof/">getsizeof</a>, <a href="/recipes/tags/memory/">memory</a>, <a href="/recipes/tags/sizeof/">sizeof</a>). Revision 21. </p> <p>This recipe determines the size of Python objects in bytes and has been further enhanced to handle ints, namedtuples, arrays and NumPy types better. Functions <em>alen</em> and <em>itemsize</em> have been updated. Support for Python 2.5 and earlier and the tests/examples have been removed. See project <a href="https://github.com/pympler/pympler">Pympler</a> for unit tests.</p> <p>See also other, simpler recipes like this <a href="http://code.activestate.com/recipes/577504">Compute memory footprint of an object and its contents</a>.</p> portalocker - Cross-platform (posix/nt) API for flock-style file locking. (Python) 2008-05-16T21:12:08-07:00Jonathan Feinberghttp://code.activestate.com/recipes/users/1511/http://code.activestate.com/recipes/65203-portalocker-cross-platform-posixnt-api-for-flock-s/ <p style="color: grey"> Python recipe 65203 by <a href="/recipes/users/1511/">Jonathan Feinberg</a> (<a href="/recipes/tags/files/">files</a>). Revision 7. </p> <p>Synopsis:</p> <p>import portalocker file = open("somefile", "r+") portalocker.lock(file, portalocker.LOCK_EX) file.seek(12) file.write("foo") file.close()</p>