UKHAS Wiki

UK High Altitude Society

User Tools

Site Tools


projects:dlistener:pythonclient
#!/usr/bin/env python2.3
#
#  radioclient.py
#  
#
#  Created by James Coxon on 14/11/2008.
#  Copyright (c) 2008 __MyCompanyName__. All rights reserved.
#
#  Version 0.2

import urllib, httplib, sys, time

def Tail(filepath, line_number, read_size=1024):
	"""
	This function returns the last line of a file.
	Args:
	filepath: path to file
	read_size:  data is read in chunks of this size (optional, default=1024)
	Raises:
	IOError if file cannot be processed.
	From: http://manugarg.blogspot.com/2007/04/tailing-in-python.html
	"""
	f = open(filepath, 'r')    # U is to open it with Universal newline support
	offset = read_size
	f.seek(0, 2)
	file_size = f.tell()
	while 1:
		if file_size < offset:
			offset = file_size
		f.seek(-1*offset, 2)
		read_str = f.read(offset)
		# Remove newline at the end
		if read_str[offset - 1] == '\n':
			read_str = read_str[0:-1]
		lines = read_str.split('\n')
		if len(lines) > 1:  # Got a line
			return lines[len(lines) - line_number]
		if offset == file_size:   # Reached the beginning
			return read_str
		offset += read_size
	f.close()

old_last_line = "1"
cmd_arg = len(sys.argv)
if cmd_arg > 2:
	user_identity = sys.argv[1]
	file_location = sys.argv[2]
	while 1:
		last_line = Tail(file_location, 2)
		log_file_dollar = last_line.split("$$")
		if len(log_file_dollar) > 1:
			last_line = log_file_dollar[1]
		print last_line
		if last_line != old_last_line:
			old_last_line = last_line
			#Simple Parsing
			#Checking sentence length + number of fields/length of individual fields
			last_line_length = len(last_line)
			last_line_split = last_line.split(",")
			last_line_field_count = len(last_line_split)
			callsign_field_length = len(last_line_split[0])
			field_too_big = 0
			last_line_check = last_line_split
			callsign = last_line_check.pop(0)
			for field in last_line_check:
				if len(field) > 10:
					print field
					print "Field too big"
					field_too_big = 1
			print last_line_length
			print last_line_split
			print last_line_field_count
			print callsign_field_length
			if last_line_length < 100 and last_line_field_count >= 8 and last_line_field_count < 14 and callsign_field_length < 30 and field_too_big == 0:
				print user_identity + "," + last_line
				#Uploads to server
				# following code from: http://www.python.org/doc/2.5.2/lib/httplib-examples.html
				params = urllib.urlencode({'identity': user_identity, 'string': last_line})
				headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}
				conn = httplib.HTTPConnection("www.robertharrison.org:80")
				conn.request("POST", "/listen/listen.php", params, headers)
				response = conn.getresponse()
				print response.status, response.reason
				data = response.read()
				conn.close()
				#time.sleep(2)
			else:
				print "Poor Data"
			#time.sleep(2)
		print "No new data"
		time.sleep(5)
else:
	print "No arguments passed"
	print "radioclient.py $username $logfile"
projects/dlistener/pythonclient.txt ยท Last modified: 2009/01/01 20:09 by jcoxon