Friday, March 27, 2015

To take a leap forward through server side developmental years, I am now working through a Django tutorial. Certain advantages, such as scalability I don't yet benefit from. Though the safe simplicity of managing changes to models is an asset I can admire. Mainly due to several years of working with a software that at times required exact modifications to SQL tables to support front end changes. The models in Django enable the user to access and alter data stored in the tables. Lucky for me I've started using Django version 1.7 which includes the migrate commands, benefits explained below.

Challenge: How to modify an existing model

Solution (Django < 1.7): This developer making a change manually would have to update the table with the exact Django expected syntax, and make perfect reflected changes to models.py. Or the developer could back up the data, wipe out the table, recreate it using syncdb, and reload the data.

Not only does Django handle update the sql table perfectly, but a file is produced in the migrations folder that shows you the exact changes made in case you would like to change any. It is possible as well to use the command sqlmigrate to view the changes Django plans to make on your table.

Of course before hand the developer could have installed South, but the point here is the wonderful benefit of the now built in functionality. The migrate commands empower the developer to make changes to the project with little work and a lot of security.

Thursday, March 19, 2015

My simple server had three requirements, listen to an address, parse information, and respond accordingly.

Challenge: Listen to an address
Solution: Initialize a socket to listen into my localhost

The first step was to import the socket class, and initialize a socket to the AF_INET family, and SOCK_STREAM type. This type is for the TCP sockets, and the AF_NET family sets up the socket object on the network by taking a two parameter tuple of host and port. The address was set to my localhost, 127.0.0.1, and the port was set to 8080. Port 80 is the default HTTP port, but it is only accessible by the root user, port 8080 is the designated HTTP alternate. The socket was then bound to the address and set to listen and establish a connection with incoming requests.

Challenge: Parse the incoming request
Solution: Use regular expressions to extract the method and path

Initially I thought all methods would be in capital letters and used the reg-ex code ^([A-Z]+) to collect and group one or more capital letters from the start of the string. Though looking into the documentation the request line only requires that the method token be followed by a space, and so the code was updated to, ^([^ ]+) which instead looks for any character that is not a string. The path was then collected by grouping content until the next space was encountered, ... (^[^ ]). The final parameter of the request line is HTTP and the version, which I matched with ... HTTP.*$ . This looks for the letters HTTP followed by anything till the end of the line. Note: The original request data was split by \r\n, so the request line was an isolated string.

Challenge: Respond accordingly
Solution: Depending on the method, if the file requested exists, and its file type send response data

My server exclusively responded to GET method requests, if the condition was met the path was checked. My files were held inside a directory titled 'www' this path was defined in the global variable WEBROOT. Once WEBROOT was appended to the path I used os.path.isfile to check if a file existed. If the file was not found a page was returned alerting the user with the 404 error code. If it tested true I assigned a content type depending on the file extension at the end of the path, and saved the length of the files content. The response data was then constructed with the content type and length added into the header, and the file appended as the body. This response data was send back over the connection.

Wednesday, March 11, 2015

Information contained in bits grouped in bytes is packaged up and shared on the internet between clients and servers. A computer can receive, send and process bits of information.

Challenge: Serve up a site and load an image that is locally stored.
Problem: Could not serve image even when the data was correctly stored
Solution: Use the necessary headers in the response variable

The first step was to get the raw format of my image, over a thousand obscure and oddly spaced out characters. I modified a small client server in python, updating the path and host, to point to the minuscule image to access the raw data. Even when all the code was correctly saved to the local folder, I could not serve the image. After some failed 'fixes', such as modifying the file type of the image, the page served correctly when I added the correct headers to the response data.

Tuesday, March 3, 2015

The solution to the 16th question posed by the Project Euler website requires a user to parse through a number that is over 300 digits long.

Problem: I need to access each digit in the number. My algorithm returned a value in scientific notation and attempts to retrieve all digits of the number failed as most methods, such as .toPrecision are limited to about 20 digits of precision.

Cause: JavaScript numbers only have 64 bits to use for storage (my number is 1001 bits)

I found a calculator that could compute my number and display all the digits using the BigInt.js library and therefore trusted it over other big integer libraries to handle the sizable number in my program. After unsuccessful attempts with the new operator I reread the documentation until I found out how to create a bigInt from a string using the str2bigInt function. Only concern is that this asked for how many bits the number needed, after exploring binary I had a better grasp on why each digit needs about 3 bits, and declared the numbers accordingly. At long last I updated my simple algorithm and submitted the solution.

"Congratulations, the answer you gave to problem 16 is correct.

You are the 127007th person to have solved this problem". . and successfully submit it to the Project Euler website.