| /ExcelPicher /Mails /Tuple |
Unable to retreive feed feed://feeds.delicious.com/v2/rss
Development Tricks
Executable .pyc files
Ever wanted to drop a .pyc file right into your web server's cgi-bin directory? Frustrated because the OS doesn't know what to do with a .pyc? (missing the #! line)
Look no further! :-) Below is a tiny Bash script to do this. "cat" your .pyc onto the end of this and drop it wherever you need direct execution of that .pyc
#!/bin/bash
exec - 3< $0 ; exec python -c 'import os,marshal ; f = os.fdopen(3, "rb") ; f.readline() ; f.readline() ; f.seek(8, 1) ; _c = marshal.load(f) ; del os, marshal, f ; exec _c' $@
Make sure that the script is just two lines -- that second line is a long one!
Theory of operation:
* The first exec opens the script on file descriptor 3, within the context of the Bash script.
* The second exec starts up Python, passing it a small script specified on the command-line.
* The command-line script first wraps a Python file object around file descriptor 3 (which is the Bash script).
* The script then skips past the Bash script source and the first 8 bytes of the appended .pyc file (the eight bytes are a header).
* The script then loads the code object from the appended .pyc file and executes it.
* The execution of the code object is what runs your script.
Note that stdin, stdout, and stderr are all retained according to what the caller of the Bash script set up. Also note that sys.argv has the correct set of arguments.
Differences from normal execution of the .pyc file:
* sys.argv[0] contains '-c' rather than the script name.
* _c has been injected into the script's namespace.
* [http://www.lyra.org/greg/small/ Python for Windoof] * http://python.sandtner.org/viewtopic.php?t=2577&highlight=unicode * http://www.amk.ca/python/simple/ * http://offog.org/code/misccode.html * http://delicious.com/popular/python * http://www.eskimo.com/~jet/python/examples/mail/smtp_email.html * http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52243 * http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/82233 * http://samba.org/junkcode/
1 import tarfile, os
2
3 destination = 'F:/test.tar.bz2'
4 fileorfoldertobackup = './pgaccess'
5
6 out = tarfile.TarFile.open(destination, 'w:bz2')
7 out.add(fileorfoldertobackup, arcname=os.path.basename(fileorfoldertobackup))
8 out.close()
1
2 dst = '%s.tar.bz2' % os.path.join(dstfolder, os.path.basename(fileorfoldertobackup))
3 out = tarfile.TarFile.open(dst, 'w:bz2')
4 out.addfile(fileorfoldertobackup, arcname=os.path.basename(fileorfoldertobackup))
1
2 import smtplib
3
4 def mail(serverURL=None, sender='', to='', subject='', text=''):
5 """
6 Usage:
7 mail('somemailserver.com', 'me@example.com', 'someone@example.com', 'test', 'This is a test')
8 """
9 headers = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % (sender, to, subject)
10 message = headers + text
11 mailServer = smtplib.SMTP(serverURL)
12 mailServer.sendmail(sender, to, message)
13 mailServer.quit()
1
2 import os,string
3 address={}
4 cmd = 'c:/windows/system32/ipconfig.exe /all' # whatever your path
5 junk, txtOut = os.popen2(cmd)
6 for x in txtOut.readlines():
7 if x.find('IP Address') != -1:
8 address.setdefault('ip',x.split(':')[-1].strip())
9 if x.find('Default Gateway') != -1:
10 address.setdefault('gate',x.split(':')[-1].strip())
11 print address['gate']
12 ip=address['gate'] #replace the old ip way with this.
13 print address['ip']
14
15
16 def tree(dir, padding, print_files=False):
17 cmd = "find '%s'" % dir
18 files = os.popen(cmd).read().strip().split('\n')
19 padding = '| '
20 for file in files:
21 level = file.count(os.sep)
22 pieces = file.split(os.sep)
23 symbol = {0:'', 1:'/'}[isdir(file)]
24 if not print_files and symbol != '/':
25 continue
26 print padding*level + pieces[-1] + symbol
Kategorie/Python || Kategorie/Snippets
Python/Snippets (last edited 2010-01-16 18:33:56 by DetlevLengsfeld)