Skip to main content

Posts

Featured Post

A Winter's tale: First trip of the year to Himachal

Recent posts

Test server that accepts GET/Post calls using Python Flask

I was trying out Flask. In the process, I made a website that behaves like a server. It can help you test GET and POST calls.

It goes like this .


fromflaskimport Flask, request app = Flask(__name__) @app.route('/method', methods=['GET', 'POST']) @app.route('/method/<wish>', methods=['GET', 'POST']) defmethod_used(wish=None): if request.method =='GET': if wish: if wish indir(request): ans =None s ="ans = str(request.%s)"% wish exec s return ans else: return'This wish is not available. The following are the available wishes: %s'% [method for method indir(request) if'_'notin method] else: return'This is just a GET method'else: return"You are using POST"
When I run this, this…

How to test POST call using Python?

I got into a situation recently where I needed to test if my POST function was indeed working.

Its easy to test GET method . You just visit the URL and that's it . But for POST, you an use PlugIns.

But unfortunately I can't do that , because my browser (admin restrictions ) doesn't allow me to install plugins . Fortunately if you have Python , you can easily do that .

The request module is a handy module when it comes to testing web responses etc. It also has support for POST methods. You can literally fill a form using this . Let's test our url .

This is our POST method. (Its a Flask code).

@app.route('/method',methods=['GET','POST'])defmethod_used():ifrequest.method=='GET':return"Method used: %s"%request.methodelse:return"You are using POST"
So if its a GET method, the response we expect is Method used: GET . And if its a POST method, we expect "You are using POST"

Now lets bring out the request module. I have ru…

Something about Python Argparse

Its been decades I have been using sys.argv for simple tasks . But the thing is, sometimes we don't realize how good something is unless we use it.

The official document got me totally confused. So I wrote my own tutorial. Let's get started.

So the module to be imported is 'argparse'. First thing to do is , create a parser object.


parser = argparse.ArgumentParser()
Then we can add what arguments we want . These are the requirements.

I want a username. I also want to set a default.  If nothing is provided, then the default user will be used. If User name is provided , then that should be used. We should tell the user about this requirement through some kind of help instruction.

parser.add_argument("-user",default='Admin')
So to input a user, one needs to use the '-user' option. 

args=parser.parse_args()
Then we need to call the above. This will create a namespace.


>>>argsNamespace(user='Admin')
Note: The name space has the attribu…

How to forward domain to www.domain if you bought a domain from godaddy

If you bought a domain recently while I write this in 2017, you probably have observed the fact that "www.domain.com" works , yet "domain.com" doesn't. This is how you redirect it properly.

Login to godaddy.com



This is what you will see under "My Products". Click on Manage beside Domains

You will see another page with you domain name. Click on the machine icon on the right top corner. Choose "Manage DNS".
 In this page, you will notice at the bottom of the page, the section with heading "Forwarding" . 
Beside "DOMAIN", click on ADD.
Beside the forward to box, write www.your-domain.com. Set true the check box below. Save. That's it. 
Test by launching "domain-name.com " . It should redirect to "www.domian-name.com". Thanks for reading.



Learning BASH: Text Processing - HEAD & TAIL

BASH continues to surprise me with it's amazing collection of simple, yet extremely useful commands. They can give you a huge boost in speed and control while working. No doubt bash along with editors like VIM are the developer's favorite combinations. Gradually , you will feel the invention of mouse as a waste since you can pretty much control everything with just your keyboard.

Today we continue with more commands that are related to Text Processing.

HEAD & TAIL command
These commands are used to get contents of a file starting from the top and bottom. Unlike the CAT command that displays the whole content of a file, these command gives you control over how much you want to see.

syntax: HEAD filename | TAIL filename
Note: By default, HEAD | TAIL shows 1st/last 10 lines of a file.

Lets say I have a text file like this.
$ cat numbers.txt 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Let's run head command without any arguments other than the filename.

$ head number…

Learning BASH: Text Processing - Cut Command

Text processing tools in Bash is a huge topic . So we will take it one command at a time.

CUT COMMAND
You might think , CUT means to remove a file from location A to location B. But as the link here says, Cut command in unix (or linux) is used to select sections of text from each line of files. You can use the cut command to select fields or columns from a line by specifying a delimiter or you can select a portion of text by specifying the range or characters. Basically the cut command slices a line and extracts the text.

The definition of CUT command in linux itself says:



Print selected parts of lines from each FILE to standard output.
I created a text file (I am on windows running Cygwin...so......) . Added a few lines.

This is the first line
This is the second
And this is not the last line
Finally we end
Good Bye

The linux help says:


 N N'th byte, character or field, counted from 1
N-from N'th byte, character or field, to end of line
N-Mfrom N'th to M'th (included) byte, charac…

Learn BASH with me in 5 mins

I just started learning Linux bash from today. From my first impression of the language, I infer that it is a language with all the basic capabilities as of an infant high level language. May be I am right or wrong.Time will tell . We will keep going and keep discovering gradually. Let's start with the usual protocol of learning a language.

The HELLO WORLD program.
How to print things in shell. This is the first thing everyone wants to know while learning any language.

Anything that is not a variable is printable . And we print/echo it using the famous ECHO keyword

$ echo hello world hello world
Printing a number.
$ echo 1 1 Printing a string with double quotes$ echo"my name is arindam" my name is arindam Printing a string with single quotes$ echo'my name is Arindam' my name is Arindam Printing a number with quotes$ echo'1' 1
Creating  Variables and recalling them.
So how can we store things. How to recall that stored value. How to change that value.

X=9…