file transfer with netcat
netcat
lets you stream bytes over the network. When used with tar
, you can transfer files and directories.
Say you have two computers — bam and boom — and you want to copy the ~/pictures/ directory from bam to boom.
First, on host boom:
netcat -l -q 0 -p 3000 | tar xzv
Second, on host bam:
tar czv ~/pictures/ | nc -q 0 boom 3000
Done.
weblink: easy file sharing
Weblink sets up a small Web server on your computer to make some of your files available as downloadable web links.
First things first
Check that you have python installed, download weblink, and type:
chmod +x ./weblink
Basic usage
To share /home/pierre/lolcat.jpg, type:
./weblink /home/pierre/lolcat.jpg
The file is accessible to you through http://localhost:8888/. On the internet, the file is downloadable at http://n.n.n.n:8888/ where n.n.n.n is your computer IP address. The link is valid until weblink is stopped.
Important: If you have a firewall, you must open port 8888. If you have a NAT router, you must configure it to forward port 8888 to your computer.
Share several files
You can share several files by passing them on the command-line or by using wildcards:
./weblink lolcat1.jpg lolcat2.jpg xkcd*.png
Password protection
To protect access to your files, you may specify a pseudo-password with the --pass
option. For example, type:
./weblink --pass abcdef lolcat.jpg
and lolcat.jpg will be available at:
http://n.n.n.n:8888/abcdef/
If you can’t think of a password, use the --randompass
option instead.
./weblink --randompass lolcat.jpg
Port number
You can run weblink on a different port with the -p
option:
./weblink -p 7777 lolcat.jpg
Kudos to the python developers for making writing weblink so easy!
pygtk + pynotify: notification from statusicon
Code snippet that displays a libnotify bubble coming from a GTK statusicon. The bubble appears when you click the icon.
[download statusicon-notification.py]
#!/usr/bin/env python import gtk import pynotify def callback(icon): notification.show() pynotify.init("Some Application or Title") notification = pynotify.Notification("Title", "body", "dialog-warning") notification.set_urgency(pynotify.URGENCY_NORMAL) notification.set_timeout(pynotify.EXPIRES_NEVER) icon = gtk.status_icon_new_from_stock(gtk.STOCK_ABOUT) icon.connect('activate', callback) notification.attach_to_status_icon(icon) gtk.main()
Cross-platform configuration file handler
This is a python class to manage a configuration file, regardless of the operating system. Configuration is a set of (key, value) that can be set and retrieved by using SimpleConfig as a dictionary. Keys and values must all be strings. Example usage:
conf = SimpleConfig('myapp') # Load configuration file if it exists conf['foo'] = '3' # Set a value conf.save() # Save the configuration file on disk conf.delete() # Delete file, it was just an example
SimpleConfig builds the configuration file path using the operating system name and the application name:
- Unix: $HOME/.appname
- Mac: $HOME/Library/Application Support/appname
- Windows: $HOMEPATH\Application Data\appname
Tested on Linux and Windows XP so far.
import os import stat from ConfigParser import SafeConfigParser from UserDict import IterableUserDict __all__ = ['SimpleConfig', 'UnsupportedOSError'] class UnsupportedOSError(Exception): pass class SimpleConfig(IterableUserDict): """Cross-platform configuration file handler. This is a class to manage a configuration file, regardless of the operating system. Configuration is a set of (key, value) that can be set and retrieved by using SimpleConfig as a dictionary. Keys and values must all be strings. Example usage:: >>> conf = SimpleConfig('myapp') # Load configuration file if it exists >>> conf['foo'] = '3' # Set a value >>> conf.save() # Save the configuration file on disk >>> conf.delete() # Delete file, it was just an example SimpleConfig builds the configuration file path using the operating system name and the application name: * Unix: $HOME/.appname * Mac: $HOME/Library/Application Support/appname * Windows: $HOMEPATH\Application Data\\appname Tested on Linux and Windows XP so far. """ section = 'main' def __init__(self, appname): """Open the configuration file and fill the dictionary. ``appname`` is the application identifier, it is used to build the configuration file path. If the file does not exist, the dictionary is left empty. """ IterableUserDict.__init__(self) self.filename = self._filename(appname) self.parser = SafeConfigParser() self.parser.read(self.filename) if not self.parser.has_section(self.section): self.parser.add_section(self.section) for name, value in self.parser.items(self.section): self.data[name] = value def save(self): """Save the configuration file on disk.""" for name, value in self.data.items(): self.parser.set(self.section, str(name), str(value)) f = open(self.filename, 'w') self.parser.write(f) f.close() def delete(self): """Delete the configuration file.""" os.chmod(self.filename, stat.S_IWRITE) os.remove(self.filename) def _filename(self, appname): # os.name is 'posix', 'nt', 'os2', 'mac', 'ce' or 'riscos' if os.name == 'posix': filename = "%s/.%s" % (os.environ["HOME"], appname) elif os.name == 'mac': filename = ("%s/Library/Application Support/%s" % (os.environ["HOME"], appname)) elif os.name == 'nt': filename = ("%s\Application Data\%s" % (os.environ["HOMEPATH"], appname)) else: raise UnsupportedOSError(os.name) return filename if __name__ == "__main__": def test(): conf = SimpleConfig("myapp") filename = conf.filename port = "3000" message = "Hello World!" conf["port"] = port conf["message"] = message conf.save() conf = SimpleConfig("myapp") assert conf["port"] == port assert conf["message"] == message conf.delete() assert not os.path.exists(filename) test()