analitics

Pages

Sunday, November 20, 2011

Simple socket server with python

It is a small example I created a server to see how it works with a client program made ​​in C + +.
I can not say that it really is a server that does not accept multiple connections.
The script worked stable sending manually entered text.
I not checked the stability for the large data streams.
It is also normal because as you see below is just one example.

import socket 
HOST = 'your-IP'
PORT = 5001
s= socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST,PORT))
s.listen(1)
conn,addr=s.accept()
print 'Conectat la =',addr
while 1:
 data = conn.recv(1024)
 if not data : 
  break
 conn.send(data)
 print data
conn.close()
The distance between me and the server was considerable.

Tuesday, October 25, 2011

The python module : spynner

On official site we see the description of the spynner module :
Spynner is a stateful programmatic web browser module for Python with Javascript/AJAX support based upon the QtWebKit framework.
The module is esay to use. With few lines of codes you can use this module to parse ,view and use web pages.
from __future__ import division
from __future__ import unicode_literals
from __future__ import print_function
import re
import spynner
from BeautifulSoup import BeautifulSoup
import time
br = spynner.Browser()
br.create_webview()
br.show()
br.set_html_parser(BeautifulSoup)
br.load("http://www.linuxtoday.com/")
br.fill("input[name=username]", "name")
br.fill("input[name=passwd]", "pass")
#br.simulate_click("input[name=submit]",True)
#br.select("IBESNA~US")
#br.select('option[value="IBESNA~US"]')
#br.wait_page_load()
br.browse()

The result is show in the image bellow:
The source code is not complete. I make this simple example just to show us how to fill some editbox.
If you wantthen you can develop this script and make more useful.
I wait your answers with source code additions and new ways of using this python module.

Saturday, October 1, 2011

Show photo albums and storage with python and gdata module

The example i show today has tow parts.
First I use module getpass and i connect to google user account.
Next I use gdata functions to print user albums and spending storage.
You can also use the gdata module to add comments, rotate photos, add photos ...
The code source is show bellow:
import getpass
import gdata.photos, gdata.photos.service
pws = gdata.photos.service.PhotosService()
username=raw_input("username - ")
password= getpass.getpass("password - ")

pws.ClientLogin(username, password)

#get all albums from account
userfeed_albums = pws.GetUserFeed(kind='album')

#print albums
print "User %s has these albums: %s" % (userfeed_albums.user.text,
[a.title.text for a in userfeed_albums.entry])

#print storage
print "%s is spending %s-Mb on storage" % \
(userfeed_albums.nickname.text, int(userfeed_albums.quotacurrent.text)/1024/1024)

Sunday, September 25, 2011

Tips and tricks with Python ...

Sometimes when using new modules, we are confronted with the following problem.
The modules come with many features that are not fully documented.
Some of them we do not know.
We can use the dir function, but in most cases the result is a huge list.
Here is a simple detection method.
$ python
Python 2.7.1 (r271:86832, Apr 12 2011, 16:16:18)
[GCC 4.6.0 20110331 (Red Hat 4.6.0-2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import gnome
>>> dirlist=dir(gnome)

I put content above command in a list.
We need to processed now.
>>> dirlist.index('ui')
Traceback (most recent call last):
File "", line 1, in
ValueError: 'ui' is not in list
>>> dirlist.index('sound_play')
118

Here I found an item. To know something about it.
>>> help(dirlist[118])
no Python documentation found for 'sound_play'

Nothing about this. Let's use the command dir.
>>> dir(dirlist[118])
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

Using the examples above, you can create a script to parse this source code and check if the functions are the same as our version.

Saturday, September 24, 2011

Creating folders and documents with gdata module

Today I played with gdata python module.
The problem that I solved it:
creating folders and documents in your Gmail account.
First you need to install gdata module.
In fedora I used:
yum install python-gdata.noarch 
Here are the first lines of source code that creates a folder named test-fedora
Python 2.7.1 (r271:86832, Apr 12 2011, 16:16:18) 
[GCC 4.6.0 20110331 (Red Hat 4.6.0-2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import gdata.docs.service
>>> my=gdata.docs.service.DocsService()
>>> my.ClientLogin('your-account@gmail.com','your-password')
>>> my.CreateFolder('test-fedora')
I tried to automate the process of creating folders and I used a list and instruction for
>>> folders=['aaa','bbb','ccc']
>>> for f in folders:
...     my.CreateFolder(f)
... 
To create a document to write more lines of code.
This is because there are many types of documents
>>> new_entry = gdata.GDataEntry()
>>> new_entry.title = gdata.atom.Title(text='fedora-test')
>>> category = my._MakeKindCategory(gdata.docs.service.DOCUMENT_LABEL)
>>> new_entry.category.append(category)
>>> created_entry = my.Post(new_entry, '/feeds/documents/private/full')
Here's a simple solution to avoid loss of mail password.
>>> import getpass
>>> username = raw_input('Please enter your username: ')
Please enter your username: user1
>>> password = getpass.getpass()
Password: 
>>> print username
user1
>>> print password
pass1
I hope you will use this