How to access various Web Services in Python

How to access various Web Services in Python

Overview A very good way of learning Python is trying to work with various Web Services API’s. How do I access web services such as Youtube, Vimeo, Twitter? In order to answer that, we will first have to get some knowledge about API’s, JSON, Data structures etc. Getting Started For those of you that have followed us, you have hopefully gained some basic Python knowledge. And for you who hasn’t, I’d suggest that you start reading our pages at the very top of the site or click on the link below that you want to read more about. Python Tutorial Basics (Overview) Dictionary Functions Lists Loops Modules Strings API : Application Programming Interface An API is a protocol intended to be used as an interface by software components to communicate with each other. An API is a set of programming instructions and standards for accessing web based software applications (such as above). With API’s applications talk to each other without any user knowledge or intervention. Often, companies like Google, Vimeo and Twitter releases it’s API to the public so that developers can develop products that are powered by its service. It is important to know that an API is a…

Browsing in Python with Mechanize

Browsing in Python with Mechanize

Browsing with Mechanize The mechanize module in Python is similar to perl WWW:Mechanize. It gives you a browser like object to interact with web pages. Here is an example on how to use it in a program. import mechanize br = mechanize.Browser() br.open(“http://www.example.com/”) Python Mechanize Cheat Sheet How to open a web browser in python Loops in Python Follow second link with element text matching regular expression response1 = br.follow_link(text_regex=r”cheeses*shop”, nr=1) assert br.viewing_html() print br.title() print response1.geturl() print response1.info() # headers print response1.read() # body To get the response code from a website, you can the response.code from mechanize import Browser browser = Browser() response = browser.open(‘http://www.google.com’) print response.code Get all forms from a website import mechanize br = mechanize.Browser() br.open(“http://www.google.com/”) for f in br.forms(): print f I found this post at http://stockrt.github.com that very accurate describes how to emulate a browser in Python using mechanize. Browsing with Python (written of Drew Stephens) #!/usr/bin/python import re from mechanize import Browser br = Browser() Ignore robots.txt br.set_handle_robots( False ) Google demands a user-agent that isn’t a robot br.addheaders = [(‘User-agent’, ‘Firefox’)] Retrieve the Google home page, saving the response br.open( “http://google.com” ) Select the search box and search for ‘foo’ br.select_form( ‘f’ )…

Subprocess and Shell Commands in Python

Subprocess and Shell Commands in Python

Subprocess Overview For a long time I have been using os.system() when dealing with system administration tasks in Python. The main reason for that, was that I thought that was the simplest way of running Linux commands. In the official python documentation we can read that subprocess should be used for accessing system commands. The subprocess module allows us to spawn processes, connect to their input/output/error pipes, and obtain their return codes. Subprocess intends to replace several other, older modules and functions, like: os.system, os.spawn*, os.popen*, popen2.* commands. Let’s start looking into the different functions of subprocess. How to Use The Envoy Wrapper How to use sh in Python Port scanner in Python subprocess.call() Run the command described by “args”. We can run the command line with the arguments passed as a list of strings (example 1) or by setting the shell argument to a True value (example 2) Note, the default value of the shell argument is False. Let’s look at two examples where we show the summary of disk usage using subprocess.call() subprocess.call([‘df’, ‘-h’]) This time we set the shell argument to True subprocess.call(‘du -hs $HOME’, shell=True) Note, the official Python documentation states a warning about using the…

How to copy and move files with Shutil

How to copy and move files with Shutil

What is Shutil? The shutil module helps you automate copying files and directories. This saves the steps of opening, reading, writing and closing files when there is no actual processing. It is a utility module which can be used to accomplish tasks, such as copying, moving, or removing directory trees How to Copy a File in Python with shutil How to Recursively Copy a Folder (Directory) in Python How to Rename (Move) a File in Python shutil. copy ( src , dest ) # Basically the unix command cp src dst. # this copies the source file to the destination directory # the destination directory has to exist # if the filename already exists there, it will be overwritten # access time and last modification time will be updated # the same filename is used # the permissions of the file are copied along with the contents. import shutil import os source = os.listdir(“/tmp/”) destination = “/tmp/newfolder/” for files in source: if files.endswith(“.txt”): shutil.copy(files,destination) ########### shutil.copyfile ( src , dest ) # copy data from src to dest # both names must be files. # copy files by name import shutil shutil.copyfile(‘/path/to/file’, ‘/path/to/other/phile’) ############ shutil.move # recursively move a file…

Python’s OS Module

Python’s OS Module

Overview The OS module in Python provides a way of using operating system-dependent functionality. The functions that the OS module provides allow you to interface with the underlying operating system that Python is running on – be that Windows, Mac or Linux. You can find important information about your location or about the process. In this post, I will show some of these functions. Python System Administration Python Programming – Python Directory Methods OS.Walk and Fnmatch in Python OS functions import os Executing a shell command os.system() Get the users environment os.environ() #Returns the current working directory. os.getcwd() Return the real group id of the current process. os.getgid() Return the current process’s user id. os.getuid() Returns the real process ID of the current process. os.getpid() Set the current numeric umask and return the previous umask. os.umask(mask) Return information identifying the current operating system. os.uname() Change the root directory of the current process to path. os.chroot(path) Return a list of the entries in the directory given by path. os.listdir(path) Create a directory named path with numeric mode mode. os.mkdir(path) Recursive directory creation function. os.makedirs(path) Remove (delete) the file path. os.remove(path) Remove directories recursively. os.removedirs(path) Rename the file or directory src to…

OS Module Overview

OS Module : Overview

OS Module The OS module in Python provides a way of using operating system dependent functionality. The os module also offers functions to find important information about your location or about the process. In this post I will show some of these functions. Python Programming – Python Directory Methods Python System Administration OS.Walk and Fnmatch in Python OS Functions import os os.system() Executing a shell command os.environ() Get the users environment os.getcwd() Returns the current working directory. os.getgid() Return the real group id of the current process. os.getuid() Return the current process’s user id. os.getpid() Returns the real process ID of the current process. os.uname() Return information identifying the current OS. os.chroot(path) Change the root directory of the current process to path os.listdir(path) Return a list of the entries in the directory by path. os.mkdir(path) Create a directory named path with numeric mode mode os.makedirs(path) Recursive directory creation function os.remove(path) Remove (delete) the file path os.removedirs(path) Remove directories recursively os.rename(src, dst) Rename the file or directory src to dst os.rmdir(path) Remove (delete) the directory path See the official Python documentation for more usage of the OS module.

OS Module os.stat ( )

OS Module : os.stat ( )

OS Stat To perform a stat system call on the given path, we can use the os.stat() functionof OS. First import the os module and then simply specify the path of the file that you want to perform the system call on. Quick Tip: How to Print a File Path of a Module Python Os.Walk() Python : OS.listdir and endswith( ) Example Let’s see an example of how to use the os.stat function import os print “-” * 30 print “os.stat = status of a file ” , os.stat(‘/usr/bin/vi’) print “-” * 30

Python Standard Library vs Python Package Index

Python Standard Library vs Python Package Index

Python Modules In this post, we’ll look at the Python Standard Library and Python Package Index Python Standard Library What it is: Collection of modules that are already on the system, thus there is no need to install them. Just import the modules you want to use. Search for a module: Python Modules PyInstaller: Package Python Applications (Windows, Mac and Linux) Python Programming – Functions Python Package Index What it is: It’s a repository of software containing more than 2400 packages created by community members.

Python Secure FTP module

Python Secure FTP module

Overview In the previous post we covered the ftplib module in Python, which you can read more about here. In this post we will cover the pysftp module. SFTP (Secure File Transfer Protocol) is used for securely exchanging files over the Internet. What is it? pysftp is an easy to use sftp module that utilizes paramiko and pycrypto. It provides a simple interface to sftp. Some of the features are: Gracefully handles both RSA and DSS private key files automatically Supports encrypted private key files. Logging can now be enabled/disabled How to use FTP in Python How to use SimpleHTTPServer How to Install Virtualenv (Python) Why should I use it? When you want to securely exchange files over the Internet. How do I install it? pysftp is listed on PyPi and can be installed using pip. # Search for pysftp pip search pysftp pysftp # – A friendly face on SFTP #Install pysftp pip install pysftp List a remote directory To connect to our FTP server, we first have to import the pysftp module and specify (if applicable) server, username and password credentials. After running this program, you should see all the files and directories of the current directory of your…

Python Range Function

Python Range Function

The Range function The built-in range function in Python is very useful to generate sequences of numbers in the form of a list. The given end point is never part of the generated list; range(10) generates a list of 10 values, the legal indices for items of a sequence of length 10. It is possible to let the range start at another number, or to specify a different increment (even negative; Sometimes this is called the ‘step’): List Comprehension in Python Python’s range() Function Explained Python Programming – Accessing Lists Range Examples >>> range(1,10) [1, 2, 3, 4, 5, 6, 7, 8, 9] # You can use range() wherever you would use a list. a = range(1, 10) for i in a: print i for a in range(21,-1,-2): print a, #output>> 21 19 17 15 13 11 9 7 5 3 1 # We can use any size of step (here 2) >>> range(0,20,2) [0, 2, 4, 6, 8, 10, 12, 14, 16, 18] >>> range(20,0,-2) [20, 18, 16, 14, 12, 10, 8, 6, 4, 2] # The sequence will start at 0 by default. #If we only give one number for a range this replaces the end of range…