Skip to content

Instantly share code, notes, and snippets.

@edwardgeorge
Created February 4, 2010 18:05
Show Gist options
  • Save edwardgeorge/294932 to your computer and use it in GitHub Desktop.
Save edwardgeorge/294932 to your computer and use it in GitHub Desktop.
os-x pastebin server and client. intended for sending text from a vm to the host computer's clipboard.
#!/usr/bin/env python
from __future__ import with_statement
import ConfigParser
import fileinput
import os
import socket
import sys
def get_config():
configfile = os.path.expanduser('~/.pbclient')
if not os.path.exists(configfile):
print >> sys.stderr, 'could not find config'
sys.exit(1)
with open(configfile) as f:
config = ConfigParser.ConfigParser()
config.readfp(f)
return config.get('server', 'host'), config.getint('server', 'port')
def send_to_server(host, port, f):
s = socket.socket()
s.connect((host, port))
for line in f:
s.send(line)
s.close()
if __name__ == '__main__':
host, port = get_config()
send_to_server(host, port, fileinput.input())
#!/usr/bin/env python
import optparse
from AppKit import NSPasteboard, NSStringPboardType
import eventlet
from eventlet.green import socket
from Growl import GrowlNotifier
growl = GrowlNotifier('pbserver', ['newpaste', 'error'])
growl.register()
def receiver(sock):
data = []
while True:
c = sock.recv(1024)
if not c:
break
data.append(c)
data = ''.join(data)
try:
pboard = NSPasteboard.generalPasteboard()
pboard.declareTypes_owner_([NSStringPboardType], None)
pboard.setString_forType_(data, NSStringPboardType)
except Exception, e:
growl.notify('error', 'Error copying data', str(e))
else:
growl.notify('newpaste', 'New paste', data[:32])
def serve(host, port):
s = socket.socket()
s.bind((host, port))
s.listen(1)
try:
while True:
sock, addr = s.accept()
eventlet.spawn(receiver, sock)
except KeyboardInterrupt, e:
pass
if __name__ == '__main__':
parser = optparse.OptionParser()
parser.add_option('-a', '--address', dest='address', help='address to listen on')
parser.add_option('-p', '--port', dest='port', help='port to listen on', type='int')
#parser.add_option('-v', '--verbose', dest='verbose', action='store_true')
options, args = parser.parse_args()
if not options.address:
parser.error('please supply an address to bind to')
if not options.port:
parser.error('please supply an port to bind to')
serve(options.address, options.port)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment