Tag Archives: linux

Python mechanize to automate WordPress login

I had to play around with Mechanize lib to automate wordpress login. Proved out to be pretty handy.

#!/usr/bin/env python

from mechanize import Browser #pip install mechanize

br = Browser()
br.set_handle_robots(False)
br.addheaders = [("User-agent","Python Script using mechanize")]

sign_in = br.open("https://www.wordpress.com/wp-login.php")  #the login url

br.select_form(nr = 0) #accessing form by their index. Since we have only one form in this example, nr =0.
br["log"] = "user@domain" #the key "username" is the variable that takes the username/email value
br["pwd"] = "passwd"    #the key "password" is the variable that takes the password value

logged_in = br.submit()   #submitting the login credentials

logincheck = logged_in.read()  #reading the page body that is redirected after successful login

print logged_in.code   #print HTTP status code(200, 404...)
print logged_in.info() #print server info
#print logincheck #printing the body of the redirected url after login

Execution:

user@server: ~/Python $ python wordpress-login.py 
200
Server: nginx
Date: Fri, 04 Mar 2016 16:45:40 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 5762
Connection: close
Vary: Accept-Encoding
X-Frame-Options: SAMEORIGIN
X-ac: 1.ams _dfw
Strict-Transport-Security: max-age=15552000
X-UA-Compatible: IE=Edge

user@server: ~/Python $

If you uncomment the last line in the script above it would print out the HTML code of the logged in page.

Python script to download all NASDAQ and NYSE ticker symbols

Below is a short python script to get all NASDAQ and NYSE common stock tickers. You can then use the resulting file to get a lot of info using yahoofinance library.

#!/usr/bin/env python

import ftplib
import os
import re

# Connect to ftp.nasdaqtrader.com
ftp = ftplib.FTP('ftp.nasdaqtrader.com', 'anonymous', 'anonymous@debian.org')

# Download files nasdaqlisted.txt and otherlisted.txt from ftp.nasdaqtrader.com
for ficheiro in ["nasdaqlisted.txt", "otherlisted.txt"]:
        ftp.cwd("/SymbolDirectory")
        localfile = open(ficheiro, 'wb')
        ftp.retrbinary('RETR ' + ficheiro, localfile.write)
        localfile.close()
ftp.quit()

# Grep for common stock in nasdaqlisted.txt and otherlisted.txt
for ficheiro in ["nasdaqlisted.txt", "otherlisted.txt"]:
        localfile = open(ficheiro, 'r')
        for line in localfile:
                if re.search("Common Stock", line):
                        ticker = line.split("|")[0]
                        # Append tickers to file tickers.txt
                        open("tickers.txt","a+").write(ticker + "\n")

Small script to calculate future value

Heres a small script I wrote to calculate the future value.

#!/usr/bin/env python

# This script calculate the future value

iv = float(raw_input("Enter initial value: "));
rate = float(raw_input("Enter interest rate: "));
times = int(raw_input("Enter the amount of years: "));

fv = iv*((1 + (rate/100))**times)

print "Final amount is: %.2f." %fv

Project Euler number 92

Below is the solution to Project Euler problem 92. Its written in python and takes about 10 minutes to reach the solution depending on your hardware. There are some comments in the code to know what the program is doing.

#!/usr/bin/env python

number_89 = 0

for z in range(10000000, 1, -1):

        def chain(z):
                var = z
                chaindigit = 0
                while (chaindigit != 1 or chaindigit != 89):
                        # Transform the number into a string
                        list =  [int(i) for i in str(var)]
                        # Multiply each list member 
                        chain = [x * y for x, y in zip(list, list)]
                        # Add the remaining list members
                        chaindigit = sum(chain)
                        var = chaindigit
                        # Check whether it ends in 89 or 1 
                        if (chaindigit == 1 or chaindigit == 89):
                                # If it ends in 89 increment the value of number_89
                                if (chaindigit == 89):
                                        global number_89
                                        number_89 = number_89 + 1
                                break
        chain(z)

print "There are", number_89,"chain numbers that end in 89."

Setting replication MySQL version 5.5

So the other day I performed an upgrade of MySQL on a linode and notice that it wont start if I kept my old my.cnf file.

[11:47:06] user@linode1: ~ $ echo 'SHOW VARIABLES LIKE "%version%";' | mysql -u username -ppassword | grep innodb
innodb_version  5.5.31
[11:47:20] user@linode1: ~ $ 
131022 08:17:56 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
131022  8:17:56 [ERROR] An old style --language value with language specific part detected: /usr/share/mysql/english/
131022  8:17:56 [ERROR] Use --lc-messages-dir without language specific part instead.
131022  8:17:56 [Note] Plugin 'FEDERATED' is disabled.
131022  8:17:56 InnoDB: The InnoDB memory heap is disabled
131022  8:17:56 InnoDB: Mutexes and rw_locks use GCC atomic builtins
131022  8:17:56 InnoDB: Compressed tables use zlib 1.2.7
131022  8:17:56 InnoDB: Using Linux native AIO
131022  8:17:56 InnoDB: Initializing buffer pool, size = 128.0M
131022  8:17:56 InnoDB: Completed initialization of buffer pool
131022  8:17:56 InnoDB: highest supported file format is Barracuda.
131022  8:17:56  InnoDB: Waiting for the background threads to start
131022  8:17:57 InnoDB: 5.5.31 started; log sequence number 1678395
131022  8:17:57 [ERROR] /usr/sbin/mysqld: unknown variable 'master-host=192.168.140.120'
131022  8:17:57 [ERROR] Aborting

Thing is the replication entries in my.cnf need to be removed/commented out and salve needs to be set up from MySQL console.

So here is my new my.cnf.

[11:34:25] xavi@linode1: ~ $ sudo grep -v “^#” /etc/mysql/my.cnf | grep -v “^$”

[client]
port            = 3306
socket          = /var/run/mysqld/mysqld.sock
[mysqld_safe]
socket          = /var/run/mysqld/mysqld.sock
nice            = 0
[mysqld]
server-id       = 2
user            = mysql
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
port            = 3306
basedir         = /usr
datadir         = /var/lib/mysql
tmpdir          = /tmp
language        = /usr/share/mysql/english
skip-external-locking
bind-address            = 192.168.137.234
key_buffer              = 16M
max_allowed_packet      = 16M
thread_stack            = 128K
thread_cache_size       = 8
myisam-recover          = BACKUP
query_cache_limit       = 1M
query_cache_size        = 16M
expire_logs_days        = 10
max_binlog_size         = 100M
[mysqldump]
quick
quote-names
max_allowed_packet      = 16M
[mysql]
[isamchk]
key_buffer              = 16M
!includedir /etc/mysql/conf.d/

[11:34:37] xavi@linode1: ~ $

We load the above file or similar depending on your configuration. Now to configure slave we log into the MySQL CLI.


mysql> STOP SLAVE;
mysql> CHANGE MASTER TO MASTER_HOST=’192.168.140.120′, MASTER_USER=’replication-user’, MASTER_PASSWORD=’password’, MASTER_LOG_FILE=’mysql-bin.000747′, MASTER_LOG_POS=75797;
mysql> START SLAVE;
[/text]

That’s it.