I’ve been trying to learn some Python and have been tinkering with the requests module. Here is how I am able to log into a webpage, such as WordPress.

import requests
 url = "https://techish.net/wp-login.php"
 redirect_to  = "https://techish.net/wp-admin/"
 with requests.Session() as session:
     post = session.post(url, data={
         'log': 'admin',
         'pwd': 'password',
         'redirect_to': redirect_to
         }, allow_redirects=True)
     get = session.get(redirect_to, cookies=post.cookies)
     print(get.text)

#python, #wordpress

Came across a script I wrote some time ago. Sitting there in my user directory was patch.cmd. As I find my scripts, I’m putting them in Git for historical purposes… and for a good laugh.

What in the actual fuck are you, patch.cmd? –Rich

One thing I learned;  document the damn things.  I had no idea what this was, but I knew it was mine because of some of the commentary in the batch script.

Anyway, this script was used to patch atmfd.dll (see: MS15-078 Bulletin).

@echo off

if NOT %~n0==patch IF NOT %~n0==unpatch goto FAILINIT

reg Query HKLMHardwareDescriptionSystemCentralProcessor

I’ve been working on a small tool to aid in removing duplicate files and as I’m going back over my roughed in code, I’m trying to optimize it for some performance gains.

This snippet of code works really well for recursively counting files given a specific path.  I originally found it at StackOverflow and slightly modified to suit my needs.

Sub ProcessFile(ByVal path As String)
        fileCounter += 1
    End Sub

    Sub ApplyAllFiles(ByVal folder As String, ByVal extension As String, ByVal fileAction As ProcessFileDelegate)
        For Each file In Directory.GetFiles(folder, extension)
            fileAction.Invoke(file)
        Next
        For Each subDir In Directory.GetDirectories(folder)
            Try
                ApplyAllFiles(subDir, extension, fileAction)
            Catch ex As Exception
            End Try
        Next

    End Sub

It processes about 27k files in 1.5 seconds on my SSD disk.  I have it running against a NAS with considerably larger amount of files, so I’ll see how well it performs.

In my sub, I use the following to kick it off.

        Dim fileCounter as Long = 0L
        Dim path = "z:"
        Dim ext = "*.*"

        ToolStripStatusLabel1.Text = "Calculating files..."

        stpw.Start()

        Dim runProcess As ProcessFileDelegate = AddressOf ProcessFile
        ApplyAllFiles(path, ext, runProcess)

        stpw.Stop()

        Dim rslts3 As String = String.Format("Total files = {0:n0}. Took {1:n0} ms.", fileCounter, stpw.ElapsedMilliseconds)
        ToolStripStatusLabel1.Text = rslts3.ToString

Graphically speaking, this isn’t much to look at – but the important part is in the ToolStripStatus. I have a timer on my form that updates the latest file count every 15 seconds so that a user would know it’s still working. Interestingly enough, if I update the ToolStripStatus with every single file that is found, it exponentially increases the time it takes to go through the files, so I decided to just update every 15 seconds.

This isn’t true API access, just a WebBrowser control. I’m looking into the API though which would require me to register my Application even though it’s not a Windows App.

It has a launcher (I haven’t decided exactly how I’ll integrate this) that will launch the Microsoft Office Online services at the click of a button.

As you can see, you can still access the Navigation provided by Microsoft. I fixed the Winform so it doesn’t launch a new window and keeps everything inside the Winform on the WebBrowser control.

I guess I see this side project being useful for someone who wants to use the free Office.com Online services with their Microsoft account and don’t want the full browser experience.

2014-08-08_141453

2014-08-08_141506

2014-08-08_141519

2014-08-08_141525

Some notes to get this to render properly in the WebBrowser control.

I had to force a User Agent to indicate I was using IE11. I think what I was seeing was that the WebBrowser control was defaulting to MSIE7. Made things look like crap.

VB.NET Code for this:

Private Property uag As String = "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko"
     _
    Private Shared Function UrlMkSetSessionOption(ByVal dwOption As Integer, ByVal pBuffer As String, ByVal dwBufferLength As Integer, ByVal dwReserved As Integer) As Integer
    End Function
    Const URLMON_OPTION_USERAGENT As Integer = &H10000001
    Public Function ChangeUserAgent(ByVal Agent As String)
        UrlMkSetSessionOption(URLMON_OPTION_USERAGENT, Agent, Agent.Length, 0)
    End Function

I implemented forcing new windows to the WebBrowser control in the Winform. If you use the top navigation at all, it opened the service in a new Internet Explorer Window.

VB.NET Code for this:

    Private Sub Webbrowser1_NewWindow(sender As Object, e As CancelEventArgs) Handles WebBrowser1.NewWindow
        WebBrowser1.Navigate(WebBrowser1.StatusText)
        e.Cancel = True
    End Sub

Originally, I wanted to just wrap this in a HTA or something simple… but I got errors indicated that I couldn’t pull this in an iFrame. So I tried some Ajax/jQuery stuff in HTA and that was a complete fail (I’m not familiar with ajax/jquery things).

This snippet of code worked when tested in VB.NET application I’m working on. It will set the User Agent string for the duration of the control.

Private Property uag As String = Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko
     _
    Private Shared Function UrlMkSetSessionOption(ByVal dwOption As Integer, ByVal pBuffer As String, ByVal dwBufferLength As Integer, ByVal dwReserved As Integer) As Integer
    End Function
    Const URLMON_OPTION_USERAGENT As Integer = &H10000001
    Public Function ChangeUserAgent(ByVal Agent As String)
        UrlMkSetSessionOption(URLMON_OPTION_USERAGENT, Agent, Agent.Length, 0)
    End Function
s
search
c
compose new post
r
reply
e
edit
t
go to top
j
go to the next post or comment
k
go to the previous post or comment
o
toggle comment visibility
esc
cancel edit post or comment