Python Web Reloader

I’m currently writing a python script to analyse web servers for connectivity. Due to various issues I have encountered whilst running this and other sites such as DDOS attacks and Mysql failing due to lack of memory. So far it probes a given site to see if it is active. If it detects connectivity it will attempt to restart the apache server.
One cool thing I learnt whilst doing this was correctly logging the output of the script using the python logging lib. The next update in this project will be to add a feature whereby it will e-mail and attach these logs for viewing by an admin should the site be down. Another idea I’m mulling  over is the ability to tail the access log of apache and detect if a site is currently under attack. Then hopefully update the IPTables to block that address.
The code so far – Note this is still in development!


import requests
import sys
import subprocess
import logging
class WebUp():
    def __init__(self):
        if(len(sys.argv) < 2) :
            print ('Usage : python webup.py http://example.com')
            sys.exit()
        self.url = sys.argv[1]

	logging.basicConfig(filename='/home/usrname/WebUp.log',format='%(asctime)s %(levelname)s:%(message)s', level=logging.DEBUG, datefmt='%d/%m/%Y %I:%M:%S %p')
    def checkURL(self):
        try:
         r = requests.head(self.url)
	 if r.status_code == 200:
	         logging.info("Website OK" + str(r.status_code))
         return r.status_code == 200
        except ( requests.exceptions.RequestException) as e:
#            print("Connection Error " + str(e))
            self.handleError(e)
    def handleError(self,e):
        # Log error 
        # Get latest tail of access.log
        # Attach to e-mail?
        # Exit
        logging.warning('Website is down due to:' + str(e))
	# restart apache
	logging.info("Attempting to restart apache2 service")
	p = subprocess.Popen("sudo service apache2 restart", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        for line in p.stdout.readlines():
		print (line)
        retval = p.wait()
	self.checkURL()
if __name__ == '__main__':
    checker = WebUp()
    checker.checkURL()


Connecting to Microsoft Access Database using Python

Source Code ->


import pypyodbc

# Create connection - make sure you change the file paths accordingly 
con = pypyodbc.connect('DRIVER={Microsoft Access Driver (*.mdb)};UID=admin;UserCommitSync=Yes;Threads=3;SafeTransactions=0;PageTimeout=5;MaxScanRows=8;MaxBufferSize=2048;FIL={MS Access};DriverId=25;DefaultDir=C:/Users/Rob/Documents/py_sql_tutorial;DBQ=C:/Users/Rob/Documents/py_sql_tutorial/Database1.mdb;')

cursor = con.cursor()

cursor.execute("SELECT customer_table.password FROM customer_table")

for row in cursor.fetchall():
    print(row)

con.close()

Android Application Development

I’ve just been digging around in some of my old projects and found my Hours Manager application.
The purpose of the application is to provide me with a way of logging the hours that I do, given that my schedule is so busy, trying to remember when I did what can often be challenging. Unfortunately  it never saw the light of day again due to my hectic schedule.

Given that again I am in a situation where having a log of my work schedule would be beneficial, of course you may be wondering, why don’t I just download one of the many good time management apps for the play store? The answer for me is simple, what would I learn from that?
Programming in Java is something new for me and I have various other android app’s that i have developed in the past although none of which have ever been polished enough to publish. Therefore I’m making a real effort to get this app on the market, not for profit just for the whole experience of releasing an application.

The challenge I have overcome today was adding the functionality to allow users to delete entries from the sql database.  To do so I wanted a dialog window to pop up when the user long pressed one of the entries in the list view.

The code that follows simply creates a new Alert Dialog window from within the SQL View fragment, from there two buttons are initialized (one to confirm and one to cancel). The database is also queried to ensure that the correct item is successfully removed, then the array adapter is notified that the data has changed. From there the list view adapter is updated with a new incarnation of the database.

Hopefully this snippet help’s someone else who’s struggling with creating a Alert Dialog within a Fragment. 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
    protected void removeRow(final long _position)
    {
        // Create a new Alert Dialog, with the fragment activity context
        AlertDialog.Builder alert = new AlertDialog.Builder(
              getActivity()
        );
        // Here we set the dialog title to that of a static string ("Delete Record?")
        alert.setTitle(R.string.dialog_title);

        // Now we open the database and query it for the current entry string - Date , Start Time, End Time etc
        openDatabase();
        alert.setMessage("Are you sure you want to remove the following entry?n" + info.getRowInfo(_position));
        closeDatabase();

        // When the confirm button is pressed
        alert.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                // Open the database again
                openDatabase();
                // Delete the entry at a specific position
                info.deleteRow(_position);
                // Tell the adapter things have changed
                adapter.notifyDataSetChanged();
                adapter.notifyDataSetInvalidated();

                // Re-populate our information in the List Adapter
                populateList();
                lv.setAdapter(adapter);

            }
        });
        // If the cancel button is pressed simply dismiss
        alert.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });
        // Ensure our database is closed
        closeDatabase();
        // Finally show the alert dialog
        alert.show();
    }
Alert Dialog displaying record information

Real Time Rendering Assignment

Real Time Rendering Techniques

Modeling the castle

First and
foremost to begin the project a suitable scene was designed using Auto Cad and modeled
using the 3DS Max software package. When modeling any scene it is essential
that everything is to scale and each element is proportional to another. This
was the reasoning behind using Auto Cad to model the scene as it an
architectural program by which designs can be made to scale.  This model provided the plans which were then
used to model the scene in 3DS Max. In order to do this two sets of drawings
were made, the first was a top down Birdseye view of the model, secondly an
elevation plan which described the structures height.

Matsumoto Castle

Many
of the castles that were researched whilst planning the project were considered
however most castles all look rather bleak, old and diminished. Therefore it
was decided that rather than your traditional European castle, a castle from
Japan named Matsumoto was planned.
A vast period of
time was then spend modeling the castle in 3ds max, as a highly detailed and
accurate model of the castle was developed. Once the model was completed,
another extended period of time was spend creating the texture maps for the
model.  One cravat to creating the model
was discovered  when a first attempt to
load the model into direct X is that as the model had multiple hi-resolution
textures the 3DS loading module unfortunately did not support the material
mapping functionality that was implemented in 3DS MAX. There were two ways
around this problem.
 Firstly, each individual component of the
castle could be separated, a new texture map could be generated and loaded in
as separate objects into DirectX. The other method would be to write a new 3DS
object loader that fully supported material mapping functionality. Due to time constraints,
rather than re-inventing the wheel, the objects of the castle were separated
into individual components and loaded into DirectX. One problem that occurred
at the beginning was due to the highly detailed models, the application took
around 30 – 60 seconds to load in all of the meshes.  Therefore in order to address this issue, many
of the complex and highly detailed mesh data was excluded and the model was
compressed into around 222,000 vertices.
In order to
texture the model each mesh was un-wrapped and a texture was individually hand
made for each one which was a time consuming process. Bump and normal maps were
applied to the Rocky and wooden features of the model to provide additional
detail.  All of the textures were then
pre-baked to include bump mapping and lighting calculations. This again took a
very long time to process.

Summer’s Here!

Just handed in my last piece of coursework for my third year in University, what a momentous occasion!
Update to follow detailing what I’ve been up to in my last term here in The University of South Wales.
Now all I have left to do is complete exams in Concurrent & Parallel Programming and Artificial Intelligence, then the fun beings!

The plan for the summer is to develop my skills in these areas :

 

  • C# programming
  • Direct X 11
  • Open GL  revising terrain project
  • Complete a few unfinished android applications
  • Build a render farm
  • Make some 3DS Max models  & possibly my own file format for use in my graphical applications
It’s certainly going to be a busy couple of months !

Mounting File Share from Terminal

After three days of hacking together mount commands finally my NAS is connected so that it can be accessed from terminal.

The script’s purpose is to mount a networked attached file store using terminal with the correct read and write attributes.

1:  ################################################################  
2:  #     Author       :   Robert Lutken                       
3:  #     E-mail  :      [email protected]                 
4:  #     Date   :      Sat, 15-02-2014                      
5:  #     File Name :     nasmount.sh                      
6:  #     Purpose  :      To connect to NAS                 
7:  #     Version      :      1.0                           
8:  #     Notes   :     Unsecure version should not be used in       
9:  #               volitile domains                 
10:  #                                          
11:  #################################################################  
12:    
13:  #!/bin/bash  
14:    
15:  ## The user name of the account on remote device i.e. admin   
16:  username=yourusername  
17:    
18:  ## The Password of the user's account on remote device  
19:  password=yourpasword  
20:    
21:    
22:  ## The source location of the server and the share directory.  
23:  ## In order to mount you should ensure that /etc/nsswitch.conf apears as so:  
24:  ## hosts:     files mdns4_minimal wins [NOTFOUND=return] dns mdns4   
25:  ## The order is important !   
26:  ## and that winsbind is installed -> sudo apt-get install winbind  
27:  ##  
28:  ## If it isn't then you may use the IP address of the local server  
29:    
30:  mountSRC=//nameofserver/sharedirectory  
31:    
32:  ## The local directory of where the shared folder should be mounted i.e. /mnt/myshare.   
33:  ## This will need to be created i.e. sudo mkdir /mnt/myshare  
34:  mountDST=/wheretomount/share  
35:    
36:  ## Finally the command that put's it all together with relvent read and write permissions.  
37:  sudo mount -t cifs $mountSRC $mountDST -o username=$username,password=$password,iocharset=utf8,file_mode=0777,dir_mode=0777  
38:    
39:    

There is however an inherent security issue with this script as it stores passwords in plain text.

I would recommend that the use of a credentials file is used :

sudo nano $HOME/Desktop/CIFSCRED  

All this file should contain is :

 username=yourusername  
 password=yourpassword  
 domain=servername  
   

Press CTRL+X and enter Y and Return to save

The file should then be restricted by using :

sudo chmod 0440 $HOME/Desktop/CIFSPWD  

Finally the original script can be updated to

1:  ################################################################  
2:  #     Author       :   Robert Lutken                       
3:  #     E-mail  :      [email protected]                 
4:  #     Date   :      Sat, 15-02-2014                      
5:  #     File Name :     nasmount.sh                      
6:  #     Purpose  :      To connect to NAS                 
7:  #     Version      :      1.                           
8:  #     Notes   :     This version uses a credentials file  
9:  #               which should be secured using :  
10:  #  
11:  #               sudo chmod 0440 myPasswordFile                           
12:  #                                          
13:  #################################################################  
14:    
15:  #!/bin/bash   
16:    
17:  myCredentials=$HOME/Desktop/CIFSPWD  
18:    
19:  ## The source location of the server and the share directory.  
20:  ## In order to mount you should ensure that /etc/nsswitch.conf apears as so:  
21:  ## hosts:     files mdns4_minimal wins [NOTFOUND=return] dns mdns4   
22:  ## The order is important !   
23:  ## and that winsbind is installed -> sudo apt-get install winbind  
24:  ##  
25:  ## If it isn't then you may use the IP address of the local server  
26:    
27:  mountSRC=//nameofserver/sharedirectory  
28:    
29:  ## The local directory of where the shared folder should be mounted i.e. /mnt/myshare.   
30:  ## This will need to be created i.e. sudo mkdir /mnt/myshare  
31:  mountDST=/wheretomount/share  
32:    
33:  ## Finally the command that put's it all together with relvent read and write permissions.  
34:  sudo mount -t cifs $mountSRC $mountDST -o credentials=$myCredentials,iocharset=utf8,file_mode=0777,dir_mode=0777  
:
nasmount
nasmount.sh