Showing posts with label Network Handling. Show all posts
Showing posts with label Network Handling. Show all posts

Wednesday, April 18, 2012

Network Handling Part 3


Hello Everyone, in this post I will discuss about accessing FTP Servers with Python.

To show you some examples of FTP, I will use the 'ftp.redhat.com'

Before I proceed, a small note here, till now we have seen that we import modules by typing "import modulename", well thats correct, but with this statement we are importing all the functions of the Module, even those which we are not using in the program, so its RAM consuming. there Is another way to import specifically whatever function we need from the Module by typing "from Modulename import specific_function"

>>> from ftplib import FTP
>>> ftp =FTP('ftp.redhat.com') <--creating a connection to redhat FTP server
>>> ftp.login() <--since logging in without username and password, there is no arguments passed here, if the login had to use a username and password, the statement ud have been ftp.login('username' 'password')
'230 Login successful.'
>>> d = 'redhat/linux' <--setting a directory path
>>> ftp.cwd(d) <--change the directory
'250 Directory successfully changed.'
>>> print ftp.dir() <--To see the contents of the directory
-rw-r--r--    1 ftp      ftp           131 Dec 20  2005 README
drwxr-xr-x    3 ftp      ftp          4096 Feb 21 05:44 beta
drwxr-xr-x    6 ftp      ftp          4096 Jun 14  2007 eal
drwxrwsr-x   14 ftp      ftp          4096 Jul 25  2011 enterprise
drwxr-xr-x    2 ftp      ftp          4096 Feb 12  2006 preview
drwxr-xr-x    2 ftp      ftp          4096 Dec 03  2008 rawhide
drwxrwsr-x    4 ftp      ftp          4096 Sep 26  2008 updates
None
>>> help(ftp)
[Try this out, as its a huge output, try to understand the functions of the Module]

There is another FTP library that is yet more powerful and robust, but external is the chilkat Module, you can go to http://www.chilkatsoft.com/ and have a look.

Well thats all for this Post. Keep Practicing, I hope you are enjoying them. Don't forget to check my next post.

Thank You!

Tuesday, April 17, 2012

Network Handling Part 2

Hello Everyone, Lets now continue our Journey, with few more examples of Network Handling in Python.

First I would like to introduce you to geturl() of urllib Module

Lets Open up the Python Shell

>>> import urllib
>>> site = "http://microsoft.org"
>>> find = urllib.urlopen(site)
>>> find.geturl()
'http://www.microsoft.com/en-us/default.aspx'
>>> # geturl() tell us the redirected url

Another Function that you would like to know is urlretrieve(), which saves a copy of any webpage, but the only thing is that it would not display any images when you open up the copied html doc. but the external links on the copied web page would just work fine.

>>> urllib.urlretrieve('http://www.python.org', 'C:/files/python.html')

So the above line would save a copy of the website in C:/files named python.html

So , we have seen the basics of accessing the web pages with Python, there are some more functions which I will discuss on the next post.

Hope this was Interesting, and Don't forget to check my next Post.

Thank You!

Monday, April 16, 2012

Network Handling Part 1

Hello Everyone, welcome to the next phase of Python, where now you will discuss about handling webpages and networks. Again, its not possible for me to provide you with each and every minute details, because in programming field, you yourself have to be creative. But I will provide you with the basics so that you can start with and go in the right direction.

By now I assume while you reading this post, that you are already aware of File Handling and other Functions of Python that has been discussed so far.

So lets get started.

For dealing with web based resources and networks, Modules that are most of the times handy are: urllib, urllib2, ftplib, httplib, etc etc, these are the standard Python Modules, but for dealing with heavy web based scripts you need to go out on the internet and search for Modules that satisfies your scripting needs.

Ok so let me show you a small script to access the  resources on a shared folder on a network, ofcourse you need to have the accessing rights from the host that you will run the script.

Lets open up the Python Shell 

>>> import os
>>> location = os.listdir('\\\\192.168.10.110\\share') <---connects to the location
>>> f = open('\\\\192.168.1.106\\share\\test.txt', 'w')
>>> f.write('hey there')
>>> f.close

This was a small script through which I could access the Local Shared Folder I am sure if you have been reading my posts carefully, you can modify this to be flexible and used by anyone. Notice I have hard coded here the path, so to make it flexible that user should be able to enter any path they like.
Note: Notice here that I have used four backslash, as to access the resource we use two backslash, we use another two backslash as escaping, if you do not escape it, you would run into an error.

Let me also show you another script through which you can access the raw HTML of any webpage.

import urllib, os
wn = raw_input("Website name: ")
connect = urllib.urlopen(wn) <--Creates a connection to the webpage
for lines in connect:
    print lines

I have saved it as connect.py and when I execute it,

>>>
Website name: http://www.google.com

[Output Not Pasted Here, as Its a huge Output]

>>>

Well thus far, you should now have some knowledge and idea about accessing web resources with Python. Hope this post was helpful.

Again, if you go out on internet for Modules, make sure its a trusted site, and use the Modules after reading and understanding it, well more on network handling on future posts.

Thank You!