Download all pages of a forum thread

I recently found myself wanting to save a thread that had over 200 pages. Saving each of them manually would have been long and painful, so I decided to create a small script in python that will save each page:

import urllib
url_prefix = "the site url prefix here"
forum = forum number
thread = thread number
pages = range(thread_start_index, thread_end_index, thread_step)
file_prefix = "file prefix here"
page_count = len(pages)
file_suffix = ".html"

for page in pages:
    url = "thread format here" % (url_prefix,forum,thread,page)
    url_handle = urllib.urlopen(url)
    page_index = (page / thread_step) + 1
    file_page = "%dof%d" % (page_index, page_count)
    filename = "%s%s%s" % (file_prefix, file_page, file_suffix)

    file_handle = open(filename, 'w')

    for lines in url_handle.readlines():
        file_handle.write(lines)

    file_handle.close()
    url_handle.close()

Django save_model not being called on create

I just spent an hour trying to figure out why my save_model was not being called on the creation of a model but got called fine during edits. It's not like the save_model code was particularly complicated or anything like that. It was, in fact, about as simple as it got:

def save_model(self,request,model,form,change):
    if not change:
        model.created_by = request.user
    model.save()

If this doesn't work, make sure you are developing against the latest Django version. A bug was reported and fixed 5-6 months ago, so you might already have the latest version, but make sure.

Find ip address of your mac

To find your Mac's IP address, type the following command in Terminal:

ifconfig

Note that it's ifconfig, not ipconfig as you might expect. You will see a bunch of data. You can find your IP address in the en1 section on the line that starts with inet.

If you're not into Terminal, you can also find it by going System Preferences > Network > Advanced > TCP/IP. You will see your IP address listed next to "IPv4 Address."

Setting up a RemoteApp

Creating a new RemoteApp is super simple once your Terminal Server is setup to allow RemoteApps. The TS RemoteApp Manager, which allows you to manage your remote applications, can be found in Control Panel > Administrative Tools > Terminal Services.

Creating a new RemoteApp

  1. Click on the "Add RemoteApp Programs" in the "Actions" menu to the right.
  2. Click next
  3. Select your program
  4. Click next
  5. Click finish

Changing properties on the RemoteApp

The RemoteApp program should show up in the "RemoteApp Programs" list at the bottom. Right-click on the RemoteApp you just created and select "Properties." You can update the location, the alias, and command line arguments.

Creating the RDP file

To create the RDP file that will let users connect to the RemoteApp, select the RemoteApp you just created. A menu should show up in the "Actions" menu specific to your application. Click on "Create .rdp File." You can choose where to save the RDP file, terminal server settings, gateway settings, and certificate settings.

Running ASP.NET applications on Windows 7 machines

You might get a weird error when you publish your .NET site to a Windows 7 machine:

HTTP Error 500.19 - Internal Server Error
The requested page cannot be accessed because the related configuration data for the page is invalid.

Config Error This configuration section cannot be used at this path. This happens when the section is locked at a parent level. Locking is either by default (overrideModeDefault="Deny"), or set explicitly by a location tag with overrideMode="Deny" or the legacy allowOverride="false".  

The fix for this requires a change to applicationHost.config, located at:

%windir%\system32\inetsrv\config\

Change the following section overrideModeDefault values from "Deny" to "Allow" for the system.webServer section in configSections:

<section name="**handlers**" overrideModeDefault="Deny" /> 
<section name="**modules**" allowDefinition="MachineToApplication" overrideModeDefault="Deny" />

You might also get the following 404 error:

The page you are requesting cannot be served because of the extension configuration. If the page is a script, add a handler. If the file should be downloaded, add a MIME map.

Module StaticFileModule
Notification ExecuteRequestHandler
Handler StaticFile
Error Code 0x80070032

You will get this error if your IIS isn't configured to run ASP.NET applications. Go to Control Panel > Programs > Turn Windows features on or off > Internet Information Services > World Wide Web Services > Application Development Features and select "ASP.NET" (which will automatically select all required dependencies).

You might then get this error:

Configuration Error 
Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately. 

Parser Error Message: Unrecognized attribute 'targetFramework'. Note that attribute names are case-sensitive.

You will get this error if your website is in .NET 4.0 but the app pool that your website is running under is targeting .NET 2.0. To fix it, go to IIS Manager > Application Pools and select your website's app pool (probably the DefaultAppPool). Click on "Basic Settings" in the "Actions" window to the right and select the right version in the ".NET Framework version" dropdown.

Finally, you mighet get this error:

Handler "PageHandlerFactory-Integrated" has a bad module "ManagedPipelineHandler" in its module list

This can happen if ASP.NET 4.0 installatoin wasn't registered correctly; you can fix it by running the following command from the cmd prompt:

%windir%\Microsoft.NET\Framework\v[version number]\aspnet_regiis.exe -i

You should finally see your website.

Display DNS cache in Windows and Mac

Whenever you visit a website, your computer needs to retrieve the IP address for that address. To improve efficiency and speed, your computer will cache the name resolutions, so that if you visit Google a second time, you don't have to ask for the IP address again.

In Windows, if you want to view your DNS cache, the command is:

ipconfig /displaydns

In Mac (Snow Leopard), you can do the same thing by:

dscacheutil -cachedump -entries Host 

And you can flush your DNS cache with:

ipconfig /flushdns

in Windows and:

dscacheutil -flushcache

in Mac.

Searching for a user in AD in .NET 3.5

.NET 3.5 brought with it the new System.DirectoryServices.AccountManagement namespace, which makes working with Active Directory so much easier. Let's take a look at how to find a user if you know their username (SAMAccountName):

private static UserPrincipal GetPrincipal(string name)
{
    var context = new PrincipalContext(
        ContextType.Domain, "yourdomain.com");

    var principal = UserPrincipal.FindByIdentity(
        context, IdentityType.SamAccountName, name);

    return principal;
}

Wow, three entire lines of code. I don't know how we'll ever convince anyone to switch from the old way of doing things. :-) How about getting the user's groups?

PrincipalSearchResult<Principal> GetGroups(UserPrincipal principal)
{
    return principal.GetAuthorizationGroups();  
}

How about you just want to dump properties about that principal?

private static IDictionary<string, string> GetProperties(UserPrincipal principal)
{
    var properties = new Dictionary<string, string>();

    var directoryEntry = principal.GetUnderlyingObject() as DirectoryEntry;

    var allProperties = directoryEntry.Properties;

    foreach (var property in allProperties.PropertyNames)
    {
        var propertyName = property.ToString();

        var value = string.Empty;

        if (allProperties[propertyName] != null && allProperties[propertyName].Count > 0)
        {
            foreach (var val in allProperties[propertyName])
            {
                if (val != null)
                {
                    value += ", " + val.ToString();
                }
            }
        }

        properties.Add(property.ToString(), value); 
    }

    return properties;
}

Change permissions of multiple files via Mac Terminal

If you want to make a file writable, it's no big deal. Open the "Info" window and update the file permissions at the bottom. However, this approach is painful if you need to change the permissions on multiple files. Enter Terminal:

chmod -R 755 name-of-the-directory-where-files-are-located

If you own the directory and the files, this will work. Otherwise, use:

sudo chmod -R 755 name-of-the-directory-where-files-are-located

The "-R" recursively sets permissions on all files and folders contained within the parent directory. The first number is the access given to the owner; the second number, to the file group's owner; and the third number, to everyone else. Here are the permissions:

  1. Read = 4
  2. Write = 2
  3. Execute = 1
  4. No access = 0

So if you want to give read, write, and execute access, the number you want is 4 + 2 + 1 = 7.

1 of 5 pages  1 2 3 >  Last »

On the Side