{"id":1160,"date":"2016-03-04T12:00:13","date_gmt":"2016-03-04T17:00:13","guid":{"rendered":"http:\/\/www.xavignu.com\/?p=1160"},"modified":"2016-04-14T11:37:18","modified_gmt":"2016-04-14T15:37:18","slug":"python-mechanize-to-automate-wordpress-login","status":"publish","type":"post","link":"https:\/\/www.xavignu.com\/?p=1160","title":{"rendered":"Python mechanize to automate WordPress login"},"content":{"rendered":"<p>I had to play around with <a href=\"https:\/\/pypi.python.org\/pypi\/mechanize\/\" target=\"_blank\">Mechanize<\/a> lib to automate <a href=\"https:\/\/wordpress.com\/\" target=\"_blank\">wordpress<\/a> login. Proved out to be pretty handy.<\/p>\n<p>[python]<br \/>\n#!\/usr\/bin\/env python<\/p>\n<p>from mechanize import Browser #pip install mechanize<\/p>\n<p>br = Browser()<br \/>\nbr.set_handle_robots(False)<br \/>\nbr.addheaders = [(&#8220;User-agent&#8221;,&#8221;Python Script using mechanize&#8221;)]<\/p>\n<p>sign_in = br.open(&#8220;https:\/\/www.wordpress.com\/wp-login.php&#8221;)  #the login url<\/p>\n<p>br.select_form(nr = 0) #accessing form by their index. Since we have only one form in this example, nr =0.<br \/>\nbr[&#8220;log&#8221;] = &#8220;user@domain&#8221; #the key &#8220;username&#8221; is the variable that takes the username\/email value<br \/>\nbr[&#8220;pwd&#8221;] = &#8220;passwd&#8221;    #the key &#8220;password&#8221; is the variable that takes the password value<\/p>\n<p>logged_in = br.submit()   #submitting the login credentials<\/p>\n<p>logincheck = logged_in.read()  #reading the page body that is redirected after successful login<\/p>\n<p>print logged_in.code   #print HTTP status code(200, 404&#8230;)<br \/>\nprint logged_in.info() #print server info<br \/>\n#print logincheck #printing the body of the redirected url after login<br \/>\n[\/python]<\/p>\n<p>Execution:<br \/>\n[shell]<br \/>\nuser@server: ~\/Python $ python wordpress-login.py<br \/>\n200<br \/>\nServer: nginx<br \/>\nDate: Fri, 04 Mar 2016 16:45:40 GMT<br \/>\nContent-Type: text\/html; charset=utf-8<br \/>\nContent-Length: 5762<br \/>\nConnection: close<br \/>\nVary: Accept-Encoding<br \/>\nX-Frame-Options: SAMEORIGIN<br \/>\nX-ac: 1.ams _dfw<br \/>\nStrict-Transport-Security: max-age=15552000<br \/>\nX-UA-Compatible: IE=Edge<\/p>\n<p>user@server: ~\/Python $<br \/>\n[\/shell]<br \/>\nIf you uncomment the last line in the script above it would print out the HTML code of the logged in page.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I had to play around with Mechanize lib to automate wordpress login. Proved out to be pretty handy. [python] #!\/usr\/bin\/env python from mechanize import Browser #pip install mechanize br = Browser() br.set_handle_robots(False) br.addheaders = [(&#8220;User-agent&#8221;,&#8221;Python Script using mechanize&#8221;)] sign_in = br.open(&#8220;https:\/\/www.wordpress.com\/wp-login.php&#8221;) #the login url br.select_form(nr = 0) #accessing form by their index. Since we have [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[62],"tags":[6,67,70],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/pTQgt-iI","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.xavignu.com\/index.php?rest_route=\/wp\/v2\/posts\/1160"}],"collection":[{"href":"https:\/\/www.xavignu.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xavignu.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xavignu.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xavignu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1160"}],"version-history":[{"count":5,"href":"https:\/\/www.xavignu.com\/index.php?rest_route=\/wp\/v2\/posts\/1160\/revisions"}],"predecessor-version":[{"id":1182,"href":"https:\/\/www.xavignu.com\/index.php?rest_route=\/wp\/v2\/posts\/1160\/revisions\/1182"}],"wp:attachment":[{"href":"https:\/\/www.xavignu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1160"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xavignu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1160"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xavignu.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1160"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}