EvilZone
Programming and Scripting => Scripting Languages => : TheWormKill July 19, 2015, 12:08:18 AM
-
I know there are plenty. I know they can do much. But I like to roll my own minimalistic solutions.
It can read from files, stdin (until an EOL is issued) and soon maybe the clipboard.
Pretty easy to use, you just need a pastebin API key (more info here (http://pastebin.com/api)), a terminal on some
Unix and something to paste.
EDIT: Of course you'll need requests, too.
To install,
$ sudo pip install requests
EDIT2:
To use the optional clipboard dependency, run
$ sudo pip install clipboard
(I might write my own tho, if someone wants).
(Or just download and install from pypi (https://pypi.python.org/pypi)).
EDIT: cleaned and updated the code a bit.
EDIT3: Incorporated kenjoe41's suggestions. Thank you.
Here's the code:
import argparse
import sys
import requests
HAS_CLIPBOARD = True
try:
import clipboard
except ImportError:
HAS_CLIPBOARD = False
def post(string, form, expiry):
post_dict = {'api_dev_key': '51652104665c18834023dae5d21d2a56',
'api_option': 'paste',
'api_paste_code': string
}
if form:
post_dict['api_paste_format'] = form
if expiry:
post_dict['api_paste_expiry_date'] = expiry
return requests.post('http://www.pastebin.com/api/api_post.php', post_dict)
def get_input(source):
if source != 'stdin':
with open(source, 'r') as f:
return f.read()
return sys.stdin.read()
if __name__ == '__main__':
ARG_PARSER = argparse.ArgumentParser(
description='Post something to pastebin.com')
ARG_PARSER.add_argument('-s', default='stdin',
help='The source of the paste (default: stdin).')
ARG_PARSER.add_argument('-f', help='The format (default: none.')
ARG_PARSER.add_argument('-e', help='The expiry time (default:none).')
ARG_PARSER.add_argument('-c', help='Use clipboard as source',
action='store_true')
ARGS = ARG_PARSER.parse_args()
if not ARGS.c:
input_string = get_input(ARGS.s)
elif HAS_CLIPBOARD:
input_string = clipboard.paste()
else:
print 'You don\'t have https://pypi.python.org/pypi/clipboard!'
sys.exit()
print '\nuploading...'
print post(input_string, ARGS.f, ARGS.e).content
-
Lets go through your code slowly and see what is wrong. Will attach further reading where possible to help you understand where i am coming from.
First, at all times when writing code to be executed as a script, use sys.exit() instead of exit(). The exit() function is meant to be used in the python's interactive shell: http://stackoverflow.com/a/6501134 (http://stackoverflow.com/a/6501134)
Second, There are some garbage collection issues in here. Pay attention to line 27:return open(source, 'r').read()
The opened file mostly becomes garbage after this call is executed and never collected till the program ends. This isn't good when it comes to big projects and in software engineering in general. It will become detrimental for programs that run for too long. It eats alot of memory. Later programming ventures might end into memory leaks, bugs and vulnerabilities so get in the habit of cleaning up.http://stackoverflow.com/a/7409814 (http://stackoverflow.com/a/7409814) and http://mortoray.com/2011/03/30/why-garbage-collection-is-not-necessary-and-actually-harmful/ (http://mortoray.com/2011/03/30/why-garbage-collection-is-not-necessary-and-actually-harmful/) Modern python programmers usually use the with statement that automatically closes the file:
with open("filename.txt", rb) as f:
content=f.read()
Read up:http://effbot.org/zone/python-with-statement.htm (http://effbot.org/zone/python-with-statement.htm)
You could also write/define an atexit (https://docs.python.org/2/library/atexit.html#module-atexit) function and call cleanup functions for that file but that might be overkill though good to learn.
We could use an option to upload an already written file so a file argument couldn't be that bad.
It is a nice little script to practice writting python version portable code. Look into the future's print_function, etc.
Here is one i wrote for ghostbin.com, read through for comparison and stuff: https://github.com/kenjoe41/ghostpaste/blob/master/ghostpaste/ghostpaste.py (https://github.com/kenjoe41/ghostpaste/blob/master/ghostpaste/ghostpaste.py)
-
Lets go through your code slowly and see what is wrong. Will attach further reading where possible to help you understand where i am coming from.
First, at all times when writing code to be executed as a script, use sys.exit() instead of exit(). The exit() function is meant to be used in the python's interactive shell: http://stackoverflow.com/a/6501134 (http://stackoverflow.com/a/6501134)
Second, There are some garbage collection issues in here. Pay attention to line 27:return open(source, 'r').read()
The opened file mostly becomes garbage after this call is executed and never collected till the program ends. This isn't good when it comes to big projects and in software engineering in general. It will become detrimental for programs that run for too long. It eats alot of memory. Later programming ventures might end into memory leaks, bugs and vulnerabilities so get in the habit of cleaning up.http://stackoverflow.com/a/7409814 (http://stackoverflow.com/a/7409814) and http://mortoray.com/2011/03/30/why-garbage-collection-is-not-necessary-and-actually-harmful/ (http://mortoray.com/2011/03/30/why-garbage-collection-is-not-necessary-and-actually-harmful/) Modern python programmers usually use the with statement that automatically closes the file:
with open("filename.txt", rb) as f:
content=f.read()
Read up:http://effbot.org/zone/python-with-statement.htm (http://effbot.org/zone/python-with-statement.htm)
You could also write/define an atexit (https://docs.python.org/2/library/atexit.html#module-atexit) function and call cleanup functions for that file but that might be overkill though good to learn.
We could use an option to upload an already written file so a file argument couldn't be that bad.
It is a nice little script to practice writting python version portable code. Look into the future's print_function, etc.
Here is one i wrote for ghostbin.com, read through for comparison and stuff: https://github.com/kenjoe41/ghostpaste/blob/master/ghostpaste/ghostpaste.py (https://github.com/kenjoe41/ghostpaste/blob/master/ghostpaste/ghostpaste.py)
Thank you for your reply! While I try to write clean code, I actually thought that GC will delete a file handle that isn't assigned to a line. And I obv didn't know about the exit() issue. Thanks and +1.