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!

0 comments:

Post a Comment