Showing posts with label urllib. Show all posts
Showing posts with label urllib. Show all posts

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!