The post How to use FTP in Python appeared first on PythonForBeginners.com.
]]>This article will show how you can use FTP in Python with the help of the
ftplib module.
The ftplib module in Python allows you to write Python programs that perform a
variety of automated FTP jobs. You can easily connect to a FTP server to retrieve
files and process them locally.
To use the ftplib module in Python, you first have to import it into your script.
To “open” a connection to the FTP Server, you have to create the object.
Once the connection is made (opened), you can use the methods in the ftplib
module.
Several methods are available in two flavors: one for handling text files and
another for binary files.
You can easily navigate the directory structure, manage and download files.
This program will first connect to a FTP server (ftp.cwi.nl) and then list the
files and directories in the FTP server root directory using the LIST() method.
from ftplib import FTP
ftp = FTP('ftp.cwi.nl') # connect to host, default port
ftp.login() # user anonymous, passwd anonymous@
ftp.retrlines('LIST') # list directory contents
Our second program opens a connection to ‘ftp.sunet.se’ as the user ‘anonymous’
with an email address of ‘[email protected]’
It then lists the files and directories on the FTP server by using the dir()
method.
The output is saved to the ‘files’ variable.
I then use print to see the files on screen.
If I want I to change directory I would just use ftp.cwd(path) to do so.
To close the FTP connection, use the quit() method.
import ftplib
ftp = ftplib.FTP('ftp.sunet.se', 'anonymous', '[email protected]')
print "File List: "
files = ftp.dir()
print files
ftp.cwd("/pub/unix") #changing to /pub/unix
Connect to the given host and port.
The default port number is 21, as specified by the FTP protocol specification.
It is rarely needed to specify a different port number.
This function should be called only once for each instance
It should not be called at all if a host was given when the instance was created.
All other methods can only be used after a connection
has been made.
The optional timeout parameter specifies a timeout in seconds for the connection
attempt.
If no timeout is passed, the global default timeout setting will be used.
Return the welcome message sent by the server in reply to the initial connection.
This message sometimes contains disclaimers or help information that may be
relevant to the user
Log in as the given user.
The passwd and acct parameters are optional and default to the empty string.
If no user is specified, it defaults to ‘anonymous’.
If user is ‘anonymous’, the default passwd is ‘anonymous@’.
This function should be called only once for each instance, after a connection
has been established.
It should not be called at all if a host and user were given when the instance
was created.
Most FTP commands are only allowed after the client has logged in.
The acct parameter supplies “accounting information”; few systems implement this.
Retrieve a file in binary transfer mode.
Command should be an appropriate RETR command: ‘RETR filename’.
The callback function is called for each block of data received, with a single
string argument giving the data block.
The optional maxblocksize argument specifies the maximum chunk size to read on
the low-level socket object created to do the actual transfer.
A reasonable default is chosen. rest means the same thing as in the transfercmd()
method.
Retrieve a file or directory listing in ASCII transfer mode.
Command should be an appropriate RETR command or a command such as LIST, NLST or
MLSD.
LIST retrieves a list of files and information about those files.
NLST retrieves a list of file names.
On some servers, MLSD retrieves a machine readable list of files and information
about those files.
The callback function is called for each line with a string argument containing
the line with the trailing CRLF stripped.
The default callback prints the line to sys.stdout.
Produce a directory listing as returned by the LIST command, printing it to
standard output.
The optional argument is a directory to list (default is the current server
directory).
Multiple arguments can be used to pass non-standard options to the LIST command.
If the last argument is a function, it is used as a callback function as for
retrlines(); the default prints to sys.stdout.
This method returns None.
Remove the file named filename from the server.
If successful, returns the text of the response, otherwise raises error_perm on
permission errors or error_reply on other errors.
Set the current directory on the server.
Create a new directory on the server.
Return the pathname of the current directory on the server.
Send a QUIT command to the server and close the connection.
This is the “polite” way to close a connection, but it may raise an exception if
the server responds with an error to the QUIT command.
This implies a call to the close() method which renders the FTP instance useless
for subsequent calls.
Close the connection unilaterally.
This should not be applied to an already closed connection such as after a
successful call to quit().
After this call the FTP instance should not be used any more.
After a call to close() or quit() you cannot reopen the connection by issuing
another login() method).
For more information, please see the official Python Documentation
The post How to use FTP in Python appeared first on PythonForBeginners.com.
]]>The post Port scanner in Python appeared first on PythonForBeginners.com.
]]>The socket module in Python provides access to the BSD socket interface. It includes the socket class, for handling the actual data channel, and functions for network-related tasks such as converting a server’s name to an address and formatting data to be sent across the network.
Sockets are widely used on the Internet, as they are behind any kind of network communications done by your computer. The INET sockets account for at least 99% of the sockets in use. The web browsers that you use open a socket and connect to the web server.
Any network communication goes through a socket.
For more reading about the socket module, please see the official documentation.
Before we get started with our sample program, let’s see some of the socket functions we are going to use.
Syntax for creating a socket
sock = socket.socket (socket_family, socket_type)
Creates a stream socket
sock = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
AF_INET
Socket Family (here Address Family version 4 or IPv4)
SOCK_STREAM Socket type TCP connections
SOCK_DGRAM Socket type UDP connections
Translate a host name to IPv4 address format
gethostbyname("host")
Translate a host name to IPv4 address format, extended interface
socket.gethostbyname_ex("host")
Get the fqdn (fully qualified domain name)
socket.getfqdn("8.8.8.8")
Returns the hostname of the machine..
socket.gethostname()
Exception handling
socket.error
How to make a simple port scanner program in Python?
This small port scanner program will try to connect on every port you define for a particular host. The first thing we must do is import the socket library and other libraries that we need.
Open up a text editor, copy & paste the code below.
Save the file as “portscanner.py” and exit the editor
#!/usr/bin/env python
import socket
import subprocess
import sys
from datetime import datetime
# Clear the screen
subprocess.call('clear', shell=True)
# Ask for input
remoteServer = raw_input("Enter a remote host to scan: ")
remoteServerIP = socket.gethostbyname(remoteServer)
# Print a nice banner with information on which host we are about to scan
print "-" * 60
print "Please wait, scanning remote host", remoteServerIP
print "-" * 60
# Check what time the scan started
t1 = datetime.now()
# Using the range function to specify ports (here it will scans all ports between 1 and 1024)
# We also put in some error handling for catching errors
try:
for port in range(1,1025):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex((remoteServerIP, port))
if result == 0:
print "Port {}: Open".format(port)
sock.close()
except KeyboardInterrupt:
print "You pressed Ctrl+C"
sys.exit()
except socket.gaierror:
print 'Hostname could not be resolved. Exiting'
sys.exit()
except socket.error:
print "Couldn't connect to server"
sys.exit()
# Checking the time again
t2 = datetime.now()
# Calculates the difference of time, to see how long it took to run the script
total = t2 - t1
# Printing the information to screen
print 'Scanning Completed in: ', total
Let’s run the program and see what the output looks like:
$ python portscanner.py
Enter a remote host to scan: www.your_host_example.com
------------------------------------------------------------
Please wait, scanning remote host xxxx.xxxx.xxxx.xxxx
------------------------------------------------------------
Port 21: Open
Port 22: Open
Port 23: Open
Port 80: Open
Port 110: Open
Port 111: Open
Port 143: Open
Port 443: Open
Port 465: Open
Port 587: Open
Port 993: Open
Port 995: Open
Scanning Completed in: 0:06:34.705170
You can observe in the above example that the port scanner scans all the ports in the device and mentions if the ports are open or closed.
In this article, we have discussed a python program using the socket module to create a port scanner. This program is intended for individuals to test their own equipment for weak security, and the author will take no responsibility if it is put to any other use.
To learn more about python programming, you can read this article on python simplehttpserver. You might also like this article on the hangman game in python.
The post Port scanner in Python appeared first on PythonForBeginners.com.
]]>The post How to use ConfigParser in Python appeared first on PythonForBeginners.com.
]]>The configparser module in Python is used for working with configuration files.
It is much similar to Windows INI files.
You can use it to manage user-editable configuration files for an application.
The configuration files are organized into sections, and each section can contain name-value pairs for configuration data.
Config file sections are identified by looking for lines starting with [ and ending with ].
The value between the square brackets is the section name, and can contain any characters except square brackets.
Options are listed one per line within a section.
The line starts with the name of the option, which is separated from the value by a colon (:) or equal sign (=).
Whitespace around the separator is ignored when the file is parsed.
A sample configuration file with section “bug_tracker” and three options would look like:
[bug_tracker]
url = http://localhost:8080/bugs/
username = dhellmann
password = SECRET
The most common use for a configuration file is to have a user or system administrator edit the file with a regular text editor to set application behavior defaults, and then have the application read the file, parse it, and act based on its contents.
An example would be the MySQL configuration file.
The script below will read the /etc/mysql/debian.cnf configuration file to get login details for MySQL, connect to MySQL and ask it for a list of all databases, go through this list calling mysqldump on each one.
This script is based on a code snippet I found on http://codepoets.co.uk/2010/python-script-to-backup-mysql-databases-on-debian/
#Importing the modules
import os
import ConfigParser
import time
# On Debian, /etc/mysql/debian.cnf contains 'root' a like login and password.
config = ConfigParser.ConfigParser()
config.read("/etc/mysql/debian.cnf")
username = config.get('client', 'user')
password = config.get('client', 'password')
hostname = config.get('client', 'host')
filestamp = time.strftime('%Y-%m-%d')
# Get a list of databases with :
database_list_command="mysql -u %s -p%s -h %s --silent -N -e 'show databases'" % (username, password, hostname)
for database in os.popen(database_list_command).readlines():
database = database.strip()
if database == 'information_schema':
continue
if database == 'performance_schema':
continue
filename = "/backups/mysql/%s-%s.sql" % (database, filestamp)
os.popen("mysqldump --single-transaction -u %s -p%s -h %s -d %s | gzip -c > %s.gz" % (username, password, hostname, database, filename))
http://www.doughellmann.com/PyMOTW/ConfigParser/
http://docs.python.org/2/library/configparser.html
The post How to use ConfigParser in Python appeared first on PythonForBeginners.com.
]]>The post Using the YouTube API in Python appeared first on PythonForBeginners.com.
]]>In this post we will be looking on how to use the YouTube API in Python. This program will show how we can use the API to retrieve feeds from YouTube. This particular script will show the most popular videos on YouTube right now.
Some of the most popular YouTube feeds are: most_recent most_viewed top_rated most_discussed top_favorites most_linked recently_featured most_responded
To start getting the data that we want from the YouTube feeds, we begin by importing the necessary modules.
import requests
import json
We make it a bit “prettier” by printing out what the program does. Then we get the feed by using the requests module. I used to use the urllib2 module to open the URL, but ever since Kenneth Reitz gave us the Requests module, I’m letting that module do most of my HTTP tasks.
r = requests.get("http://gdata.youtube.com/feeds/api/standardfeeds/top_rated?v=2&alt=jsonc")
r.text
After we have got the feed and saved it to the variable “r”, we convert it into a Python dictionary.
data = json.loads(r.text)
Now, we have a use a for loop to go through the data
for item in data['data']['items']:
This can sometimes be the tricky part and you need to look carefully how the structure is presented to you. Using a JSON editor will make it easier.
This script will show the most popular videos on YouTube.
# Import the modules
import requests
import json
# Make it a bit prettier..
print "-" * 30
print "This will show the Most Popular Videos on YouTube"
print "-" * 30
# Get the feed
r = requests.get("http://gdata.youtube.com/feeds/api/standardfeeds/top_rated?v=2&alt=jsonc")
r.text
# Convert it to a Python dictionary
data = json.loads(r.text)
# Loop through the result.
for item in data['data']['items']:
print "Video Title: %s" % (item['title'])
print "Video Category: %s" % (item['category'])
print "Video ID: %s" % (item['id'])
print "Video Rating: %f" % (item['rating'])
print "Embed URL: %s" % (item['player']['default'])
print
Using string formatting makes it a lot easier to view the code.
# Sample Output ------------------------------ This will show the Most Popular Videos on YouTube ------------------------------ Video Title: PSY - GANGNAM STYLE (?????) M/V Video Category: Music Video ID: 9bZkp7q19f0 Video Rating: 4.614460 Embed URL: http://www.youtube.com/watch?v=9bZkp7q19f0&feature=youtube_gdata_player Video Title: PSY - GENTLEMAN M/V Video Category: Music Video ID: ASO_zypdnsQ Video Rating: 4.372500 Embed URL: http://www.youtube.com/watch?v=ASO_zypdnsQ&feature=youtube_gdata_player Video Title: MACKLEMORE & RYAN LEWIS - THRIFT SHOP FEAT. WANZ (OFFICIAL VIDEO) Video Category: Music Video ID: QK8mJJJvaes Video Rating: 4.857624 Embed URL: http://www.youtube.com/watch?v=QK8mJJJvaes&feature=youtube_gdata_player
To use another YouTube standard feed, simple replace the feed in the URL: http://gdata.youtube.com/feeds/api/standardfeeds/most_responded?v=2&alt=jsonc You may have to modify the script to get the expected result.
The post Using the YouTube API in Python appeared first on PythonForBeginners.com.
]]>The post Get the Geo Location of an IP Address appeared first on PythonForBeginners.com.
]]>We are using the geody web service to find geolocate the IP.
import re
import sys
import urllib2
import BeautifulSoup
usage = "Run the script: ./geolocate.py IPAddress"
if len(sys.argv)!=2:
print(usage)
sys.exit(0)
if len(sys.argv) > 1:
ipaddr = sys.argv[1]
geody = "http://www.geody.com/geoip.php?ip=" + ipaddr
html_page = urllib2.urlopen(geody).read()
soup = BeautifulSoup.BeautifulSoup(html_page)
# Filter paragraph containing geolocation info.
paragraph = soup('p')[3]
# Remove html tags using regex.
geo_txt = re.sub(r'<.*?>', '', str(paragraph))
print geo_txt[32:].strip()
This script is copied from this post on snipplr.com
The post Get the Geo Location of an IP Address appeared first on PythonForBeginners.com.
]]>The post Script : Get the username from a prompt… appeared first on PythonForBeginners.com.
]]>#!/usr/bin/env python
#get the username from a prompt
username = raw_input("Login: >> ")
#list of allowed users
user1 = "Jack"
user2 = "Jill"
#control that the user belongs to the list of allowed users
if username == user1:
print "Access granted"
elif username == user2:
print "Welcome to the system"
else:
print "Access denied"
The post Script : Get the username from a prompt… appeared first on PythonForBeginners.com.
]]>The post Python : OS.listdir and endswith( ) appeared first on PythonForBeginners.com.
]]>When the for loop finds a match it adds it to the list “newlist” by using the append function.
import os
items = os.listdir(".")
newlist = []
for names in items:
if names.endswith(".txt"):
newlist.append(names)
print newlist
The post Python : OS.listdir and endswith( ) appeared first on PythonForBeginners.com.
]]>The post Using the web browser in Python appeared first on PythonForBeginners.com.
]]>Under most circumstances, simply calling the open() function from this module will do the right thing.
IN Unix, graphical browsers are preferred under X11, but text-mode browsers will be used if graphical browsers are not available or an X11 display isn’t available.
If text-mode browsers are used, the calling process will block until the user exits the browser.
webbrowser.open_new(url)
Open url in a new window of the default browser, if possible, otherwise,
open url in the only browser window.
webbrowser.open_new_tab(url)
Open url in a new page (“tab”) of the default browser, if possible,
otherwise equivalent to open_new().
This example will ask the user to enter a search term. A new browser tab will open with the search term in googles search field.
import webbrowser
google = raw_input('Google search:')
webbrowser.open_new_tab('http://www.google.com/search?btnG=1&q=%s' % google)
The script webbrowser can be used as a command-line interface for the module. It accepts an URL as the argument.
It accepts the following optional parameters:
The post Using the web browser in Python appeared first on PythonForBeginners.com.
]]>The post OS.walk in Python appeared first on PythonForBeginners.com.
]]>OS.walk() generate the file names in a directory tree by walking the tree either top-down or bottom-up.
For each directory in the tree rooted at directory top (including top itself), it yields a 3-tuple (dirpath, dirnames, filenames).
root : Prints out directories only from what you specified
dirs : Prints out sub-directories from root.
files: Prints out all files from root and directories
Having that information we can create a simple script doing just that. This script will print out all directories, sub-directories and files from the path I specified (/var/log)
import os
print "root prints out directories only from what you specified"
print "dirs prints out sub-directories from root"
print "files prints out all files from root and directories"
print "*" * 20
for root, dirs, files in os.walk("/var/log"):
print root
print dirs
print files
The second examples extends the first one with showing how much every file consumes using the getsize function.
print "This is using getsize to see how much every file consumes"
print "---------------"
from os.path import join, getsize
for root, dirs, files in os.walk('/tmp'):
print root, "consumes",
print sum([getsize(join(root, name)) for name in files]),
print "bytes in", len(files), "non-directory files"
The post OS.walk in Python appeared first on PythonForBeginners.com.
]]>The post Monitor Apache / Nginx Log File appeared first on PythonForBeginners.com.
]]>
This small script will count the number of hits in a Apache/Nginx log file.
This script can easily be adapted to any other log file.
The script starts with making an empty dictionary for storing the IP addresses andcount how many times they exist.
Then we open the file (in this example the Nginx access.log file) and read the
content line by line.
The for loop go through the file and splits the strings to get the IP address.
The len() function is used to ensure the length of IP address.
If the IP already exists , increase by 1.
ips = {}
fh = open("/var/log/nginx/access.log", "r").readlines()
for line in fh:
ip = line.split(" ")[0]
if 6 < len(ip) <=15:
ips[ip] = ips.get(ip, 0) + 1
print ips
If you now browse to your website, and run the python script, you should see your IP address + the counts.
The post Monitor Apache / Nginx Log File appeared first on PythonForBeginners.com.
]]>