Friday, 5 October 2018

Gmail API and Python - Resolved - SMTPAuthenticationError (534, '5.7.14') using Gmail API and Python


While working on Gmail API and integrating it in a project using Python, I came across this error "SMTPAuthenticationError (534, '5.7.14')" whose solution was not so obvious. I found many people facing the same problem on different forums so I ended up writing a post for future reference. The exact error was:

"(534, '5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbsl\n5.7.14 ....
> Please log in via\n5.7.14 your web browser and then try again.\n5.7.14  Learn more at\n5.7.14  https://support.google.com/mail/answer/78754 g65-v6sm13547202pfg.98 - gsmtp')"

You need to check and verify following things:

STEP 1:
Recheck your username and password (I know its obvious but still you might be making some mistake while entering the password)

STEP 2:
Check if you have enabled less secure apps by navigating to:
 https://www.google.com/settings/security/lesssecureapps

STEP 3 (Important):
Disabling Captcha for your Client App which is accessing the Gmail API. Using your browser, login to your Gmail Account and navigate to the link:
http://www.google.com/accounts/DisplayUnlockCaptcha

Click the continue button. You will see the following message:
"Account access enabled Please try signing in to your Google account again from your new device or application."

Its been found that google resets this setting of UnlockCaptcha if there are any changes in the Gmail Account Settings and therefore the error SMTPAuthenticationError (534, '5.7.14') occurs. You need to go through STEP 3 again so that your App can access Gmail API and send mail.

Thursday, 18 January 2018

How To Secure Your Redis Installation on CentOS 6.x with Password Authentication | Configuring a Redis Password for CentOS




Using CentOS 6.x, for applying Password Authentication in Redis through requirepass Configuration, we need to edit the redis.conf file.

Open the configuration file(name of the file may vary in your machine):

/etc/redis/6379.conf

Find # requirepass foobared and change it in following way:
requirepass yourpassword
Save your changes and restart redis-server
service redis_6379 restart
redis-cli
127.0.0.1:6379>set test "TestEntry"
Since we have applied password in Redis Configurtion file "/etc/redis/6379.conf" , we will get NOAUTH error as follows:
(error) NOAUTH Authentication required.
while being in redis-cli, write following command with your password for authentication:
127.0.0.1:6379> auth yourpassword
OK
And then when you will set any value, it will return OK
127.0.0.1:6379> set test "TestEntry"
OK
We can also run redis-cli directly with password authentication by using following command:
redis-cli -a "yourpassword"
We get OK when we run redis-cli in this way and set any value:
127.0.0.1:6379> set test2 "TestEntry2"
OK

Tuesday, 16 January 2018

Python - HTML to Text for sending SMS - SMS Safe characters - remove \xa0


Following code are some of the alternatives for removing special characters from string:
from bs4 import BeautifulSoup

raw_html = 'Dear Parent,
This is a test message, kindly ignore it. 
Thanks
' clean_text = BeautifulSoup(raw_html, "lxml").text print clean_text #u'Dear Parent,\xa0This is a test message,\xa0kindly ignore it.\xa0Thanks'
The above code produces these characters \xa0 in the string. To remove them properly, we can use two ways. The first one is BeautifulSoup's get_text method with strip argument as True
clean_text = BeautifulSoup(raw_html, "lxml").get_text(strip=True)

print clean_text
# Dear Parent,This is a test message,kindly ignore it.Thanks

The other option is to use python's library unicodedata
import unicodedata

clean_text = BeautifulSoup(raw_html, "lxml").text
print clean_text
#u'Dear Parent,\xa0This is a test message,\xa0kindly ignore it.\xa0Thanks'

new_str = unicodedata.normalize("NFKD",clean_text)
print new_str
# u'Dear Parent,This is a test message,kindly ignore it.Thanks'

Wednesday, 27 September 2017

Django Python Web App Development - Python XHTML2PDF Library error FIXED - "ImportError: cannot import name inputstream django" Solved

cannot import name inputstream, Django, Django alirazabhayani, Django Full Stack Development ali raza bhayani, html5lib, inputstream, Python, Python alirazabhayani, xhtml2pdf

While working on a Django Web Application using Python, I recently encountered an error while using a library named xhtml2pdf. This library is a Python library for exporting xhtml to PDF.

While using it in Django Web framework for developing a hassle free web service to export customized HTML templates into PDF, I encountered some error which I had to dig into the library to find the actual problem and its solution. I noticed that this problem of "cannot import name inputstream django" was faced by many people and therefore writing this post to document it. Please note that this proposed solution is workable for Python 2.7 and hasn't been checked on Python v.3.x .

When I installed xhtml2pdf library and used it in Django Web framework, I encountered following error:
 importerror: cannot import name inputstream 
Following trace shows that the error is actually triggered from html5lib which is one of the dependencies of xhtml2pdf:
File "/usr/home/username/virtualenvs/projectname/lib/python2.7/site-packages/xhtml2pdf/parser.py", line 17, in 
    from html5lib import treebuilders, inputstream 
ImportError: cannot import name inputstream
Due to broken dependencies of xhtml2pdf and lack of documentation, I had to dig in the module which revealed that this error was coming due to the fact that xhtml2pdf requires a specific version of html5lib to work properly. To solve this issue, I installed html5lib's version 1.0b8 in the following way:
$ pip install html5lib==1.0b8
 
If you had previously installed html5lib's different version, you will have to uninstall it first by running following command on your terminal or virtual environment:
$ pip uninstall html5lib

Thursday, 20 October 2016

Adding IP aliases in FreeBSD - Freebsd ifconfig add Remove Alias IPv4 / IPv6 / inet6

FreeBSD Assign IPv4 address
: # ifconfig em0 inet 192.168.10.5 netmask 255.255.255.0
FreeBSD ifconfig add alias for IPv4
: # ifconfig em0 inet 192.168.10.10/24 add
FreeBSD ifconfig remove alias for IPv4
: # ifconfig em0 inet 192.168.10.10/24 -alias
For IPv6
FreeBSD Enable IPv6 functionality of the interface:
: # ifconfig em0 inet6 -ifdisabled
FreeBSD Add the IPv6 address 2001:DB8:DBDB::123/48 to the interface em0:
: # ifconfig em0 inet6 2001:db8:bdbd::123 prefixlen 48 alias
FreeBSD ifconfig remove/delete Alias for IPv6 inet6
: # ifconfig em0 inet6 2001:db8:bdbd::123/48 delete

Monday, 22 August 2016

PostgreSQL setval Sequence - PostgreSQL manually alter sequence - Reset sequence of setval in PostgreSQL - Ali Raza Bhayani


In one of the my PostgreSQL tables, I used a very handy builtin Sequence Manipulation function setval() of PostgreSQL to generate Primary Keys in a sequential and controlled manner. But during a migration, I wanted to identify new records by Primary Key sequence greater than thousand. A very handy and tested recipe to restart the setval() function sequence for such cases is by using RESTART WITH in the following way:
ALTER SEQUENCE sequence_name RESTART WITH 1000;
is equivalent to:
    SELECT setval('sequence_name', 1000, FALSE);
Either of the statements may be used to restart the sequence and you can get the next value by:
    nextval('sequence_name')
The above recipe can also be used to reset PostgreSQL primary key sequence when it falls out of sync.

PostgreSQL setval Sequence - PostgreSQL manually alter sequence - Reset sequence of setval in PostgreSQL - Ali Raza Bhayani


In one of the my PostgreSQL tables, I used a very handy builtin Sequence Manipulation function setval() of PostgreSQL to generate Primary Keys in a sequential and controlled manner. But during a migration, I wanted to identify new records by Primary Key sequence greater than thousand. A very handy and tested recipe to restart the setval() function sequence for such cases is by using RESTART WITH in the following way:
ALTER SEQUENCE seq RESTART WITH 1000;
is equivalent to:
    SELECT setval('sequence_name', 1, FALSE);
Either of the above statements may be used to restart the sequence and for getting the next value following statement can be used:
    nextval('sequence_name')
The above recipe can also be used to reset PostgreSQL primary key sequence when it falls out of sync.