Word of Sketchground |
Hi, I'm Jens. This is my personal blog featuring whatever comes to my mind. It will probably be mostly techie stuff related to Android, OSX, Java and whatever is my hobby at said time. |
I was looking through various posts on django on nginx and couldn’t find any init scripts for easily starting / stopping the django fcgi server.
Therefore i wrote up this one. There’s still room for improvements though, for example forcing execution under a www user or such.
#!/usr/bin/env python
import sys
import argparse
import os
PROJECT_LOCATION = "/path/to/django/project"
ENVIRONMENT_LOCATION = "/path/to/virtual/env" # or path to python binary..
PID_LOCATION = "/var/log"
def check_pid(pid):
try:
os.kill(pid,0)
except OSError:
return False
return True
def check_pid_file():
try:
file = open(PID_LOCATION+"/djangofcgi.pid", 'r')
pid = file.readline()
return check_pid(int(pid)), int(pid)
except IOError:
return False, None
def start():
running, pid = check_pid_file()
if not running:
os.system(ENVIRONMENT_LOCATION + "/bin/python "+PROJECT_LOCATION+"/manage.py runfcgi method=threaded pidfile="+PID_LOCATION+"/djangofcgi.pid host=127.0.0.1 port=8801")
print "Started."
else:
print "Django fcgi already running with pid: " + pid
def stop():
running, pid = check_pid_file()
if running or (not running and pid is not None):
try:
os.kill(pid, 9)
except OSError:
print "process doesn't exist"
os.remove(PID_LOCATION+"/djangofcgi.pid")
def main():
parser = argparse.ArgumentParser(description="start | stop | restart to run django fastcgi")
parser.add_argument("operation")
args = parser.parse_args()
if args.operation == "start":
start()
print "starting"
elif args.operation == "stop":
stop()
print "stopping"
elif args.operation == "restart":
stop()
start()
print "restarting"
elif args.operation == "isrunning":
status, pid = check_pid_file()
print status
else:
print "Usage: serve.py start, stop, restart or isrunning"
if __name__ == "__main__":
main()
All that is needed now is a cron job that checks if the service is running, and if it’s not then start it.
Today i scanned in an article that was printed in duplex. I managed to scan it in a silly way that created one PDF with pages 1, 3, 5, 7, 9… and another PDF with pages …10,8,6,4,2. The total page count was about 66 pages and i couldn’t find any tools for weaving PDF’s together in a custom way other than batch merging with one PDF after the other.
This led me to implement a simple java program using pdfbox from apache. The code is crude but gets the job done. It expects both documents to be the same length, as well as the first one being forward merged and the second backwards merged.
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.exceptions.COSVisitorException;
import java.io.IOException;
import java.util.List;
public class Fix {
public static void main(String[] args) {
if(args == null || args.length != 2) {
System.out.println("Usage: java Fix \n filename1 is forward merged, filename2 is backward merged")
}
try{
PDDocument doc1 = PDDocument.load(args[0]);
PDDocument doc2 = PDDocument.load(args[1]);
PDDocumentCatalog cat1 = doc1.getDocumentCatalog();
PDDocumentCatalog cat2 = doc2.getDocumentCatalog();
System.out.println(doc1.getNumberOfPages());
System.out.println(doc2.getNumberOfPages());
PDDocument mergedDoc = new PDDocument();
List pages1 = cat1.getAllPages();
List pages2 = cat2.getAllPages();
for(int i = 0, j = pages2.size() - 1; i < pages1.size(); i++, j--) {
mergedDoc.addPage((PDPage)pages1.get(i));
mergedDoc.addPage((PDPage)pages2.get(j));
}
System.out.println(mergedDoc.getNumberOfPages());
mergedDoc.save("merged.pdf");
doc1.close();
doc2.close();
mergedDoc.close();
} catch(IOException e) {
System.out.println("ERR!!!");
} catch(COSVisitorException e) {
System.out.println("ERR 2!!!");
}
}
}
A generic tool that supports flexible merging would be more useful and i might expand it once i get the time.
Edit: I finally got around to pretty print the source. Thank you highlight.js.
I have decided to take part in the Java4K game competition. Albeit being out a little late i believe i can still make a decent effort as 4096 bytes limits the size of the game itself.
It’s gonna be a top down shooter with boss battles only. Further details will be revealed when development progresses and if they fit into the 4096 bytes.
Current jar file is 2533 bytes and looks like this.

I’m running out of space fast and still don’t have logic, bullet patterns and graphics on boss..
In my efforts to expose some webservices at a stock exchange site for my own app, i needed some software for sniffing http packets.
One “http sniffer osx” search on google revealed what should be my target for reverse engineering. The software had a bug so the 14 day trial was already expired upon install. A serial seemed to be more difficult than i expected so i decided to try out reverse engineering 101.
dylanedwards gave me a good introduction for what to look at and on i went.
I used OTX for disassembly as it gives a nicer output than apples own otool. For hex editing i use Hex fiend. No more software is needed.
Patching said software were done two places. First to get the nagging screen away and second to circumvent license check on the scoop button.
Noticing (long)encryptedAntiRegBypassCode in the startCapture method i knew something fishy was going on.
-(void)[AppController startCapture]
+1172 00005b3a 0f85da010000 jnel 0x00005d1a
as seen above, next interesting line was the jump if not equal. Basically this jumps to the end of the method without performing the rest of the scoop buttons functionality.
I ended up jumping to offset 00005b3a in the binary using Hex Fiend and replacing 0f85da010000 with 909090909090 which are a bunch of nop’s.
A broken trial version led to an unlimited trial version. Let’s see if the software is good enough to buy after 14 days ;)