Parse a log file

2010-01-18 Thread kak...@gmail.com
Hello to all!
I want to parse a log file with the following format for
example:
  TIMESTAMPEOperation FileName
Bytes
12/Jan/2010:16:04:59 +0200   EXISTS   sample3.3gp   37151
12/Jan/2010:16:04:59 +0200  EXISTSsample3.3gp   37151
12/Jan/2010:16:04:59 +0200  EXISTSsample3.3gp   37151
12/Jan/2010:16:04:59 +0200  EXISTSsample3.3gp   37151
12/Jan/2010:16:04:59 +0200  EXISTSsample3.3gp   37151
12/Jan/2010:16:05:05 +0200  DELETE  sample3.3gp   37151

How can i count the operations for a month(e.g total of 40 Operations,
30 exists, 10 delete?)
Any tips?

Thanks in advance
Antonis
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Parse a log file

2010-01-18 Thread kak...@gmail.com
On Jan 18, 11:56 pm, Tim Chase  wrote:
> kak...@gmail.com wrote:
> > I want to parse a log file with the following format for
> > example:
> >               TIMESTAMPE            Operation     FileName
> > Bytes
> > 12/Jan/2010:16:04:59 +0200   EXISTS       sample3.3gp   37151
> > 12/Jan/2010:16:04:59 +0200  EXISTS        sample3.3gp   37151
> > 12/Jan/2010:16:04:59 +0200  EXISTS        sample3.3gp   37151
> > 12/Jan/2010:16:04:59 +0200  EXISTS        sample3.3gp   37151
> > 12/Jan/2010:16:04:59 +0200  EXISTS        sample3.3gp   37151
> > 12/Jan/2010:16:05:05 +0200  DELETE      sample3.3gp   37151
>
> > How can i count the operations for a month(e.g total of 40 Operations,
> > 30 exists, 10 delete?)
>
> It can be done pretty easily with a regexp to parse the relevant
> bits:
>
>    import re
>    r = re.compile(r'\d+/([^/]+)/(\d+)\S+\s+\S+\s+(\w+)')
>    stats = {}
>    for line in file('log.txt'):
>      m = r.match(line)
>      if m:
>        stats[m.groups()] = stats.get(m.groups(), 0) + 1
>    print stats
>
> This prints out
>
>    {('Jan', '2010', 'EXISTS'): 5, ('Jan', '2010', 'DELETE'): 1}
>
> With the resulting data structure, you can manipulate it to do
> coarser-grained aggregates such as the total operations, or remap
> month-name abbreviations into integers so they could be sorted
> for output.
>
> -tkc

Thank you both so much

Antonis
-- 
http://mail.python.org/mailman/listinfo/python-list


Text file to XML representation

2009-10-21 Thread kak...@gmail.com
Hello,
I would like to make a program that takes a text file with the
following representation:

outlook = sunny
|   humidity <= 70: yes (2.0)
|   humidity > 70: no (3.0)
outlook = overcast: yes (4.0)
outlook = rainy
|   windy = TRUE: no (2.0)
|   windy = FALSE: yes (3.0)

and convert it to xml file for example:























Is there a way to do it?
Please help
Thanks in advance
Antonis
-- 
http://mail.python.org/mailman/listinfo/python-list


Comparing file last access date

2010-02-14 Thread kak...@gmail.com
Hi to all,
what i want is to search a folder, and if the last access date of the
files in that folder is greater than, lets say 7 days, those files
deleted. (Somekind of a file cleaner script)
I had problems with converting

now = today = datetime.date.today()
and
stats = os.stat(file)
lastAccessDate = time.localtime(stats[7])

into matching formats so that
 if (now - lastAccessDate) > 7:
 delete the file

what i do wrong
Thanks in advance
Antonis
-- 
http://mail.python.org/mailman/listinfo/python-list


HTTP Post Request

2010-05-10 Thread kak...@gmail.com
Hi to all, i want to ask you a question, concerning the best way to do
the following as a POST request:
There is server-servlet that accepts xml commands
It had the following HTTP request headers:

Host: somehost.com
User-Agent: Jakarta Commons-HttpClient
Content-Type: text/xml
Content-Length: 415

and the following request body (reformatted here for clarity):



  search

How can i send the above to the Listener Servlet?
Thanks in advance
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: HTTP Post Request

2010-05-10 Thread kak...@gmail.com
On May 10, 10:22 am, Kushal Kumaran 
wrote:
> On Mon, May 10, 2010 at 7:30 PM, kak...@gmail.com  wrote:
> > Hi to all, i want to ask you a question, concerning the best way to do
> > the following as a POST request:
> > There is server-servlet that accepts xml commands
> > It had the following HTTP request headers:
>
> >            Host: somehost.com
> >            User-Agent: Jakarta Commons-HttpClient
> >            Content-Type: text/xml
> >            Content-Length: 415
>
> > and the following request body (reformatted here for clarity):
>
> >            
> >            
> >              search
> >            
> > How can i send the above to the Listener Servlet?
> > Thanks in advance
>
> Use the xmlrpclib module.
>
> --
> regards,
> kushal

OK, sending headers with xmlrpclib,
but how do i send the XML message?

Thanks
A.K.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: HTTP Post Request

2010-05-11 Thread kak...@gmail.com
On May 11, 5:06 am, Kushal Kumaran 
wrote:
> On Mon, May 10, 2010 at 8:26 PM, kak...@gmail.com  wrote:
> > On May 10, 10:22 am, Kushal Kumaran 
> > wrote:
> >> On Mon, May 10, 2010 at 7:30 PM, kak...@gmail.com  wrote:
> >> > Hi to all, i want to ask you a question, concerning the best way to do
> >> > the following as a POST request:
> >> > There is server-servlet that accepts xml commands
> >> > It had the following HTTP request headers:
>
> >> >            Host: somehost.com
> >> >            User-Agent: Jakarta Commons-HttpClient
> >> >            Content-Type: text/xml
> >> >            Content-Length: 415
>
> >> > and the following request body (reformatted here for clarity):
>
> >> >            
> >> >            
> >> >              search
> >> >            
> >> > How can i send the above to the Listener Servlet?
> >> > Thanks in advance
>
> >> Use the xmlrpclib module.
>
> > OK, sending headers with xmlrpclib,
> > but how do i send the XML message?
>
> Your XML message is an XML RPC message.  You will use xmlrpclib like this:
>
> server_proxy = xmlrpclib.ServerProxy(('somehost.com', 80))
> result = server_proxy.search()
>
> The call to server_proxy.search will result in an actual XML RPC
> message being sent.
>
> Read up on the xmlrpclib documentation 
> here:http://docs.python.org/library/xmlrpclib.html, and the XMLRPC spec
> here:http://www.xmlrpc.com/spec
>
> --
> regards,
> kushal

Ok I got it!
Thank you!!!

A.K
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: HTTP Post Request

2010-05-11 Thread kak...@gmail.com
On May 11, 10:56 am, "kak...@gmail.com"  wrote:
> On May 11, 5:06 am, Kushal Kumaran 
> wrote:
>
>
>
> > On Mon, May 10, 2010 at 8:26 PM, kak...@gmail.com  wrote:
> > > On May 10, 10:22 am, Kushal Kumaran 
> > > wrote:
> > >> On Mon, May 10, 2010 at 7:30 PM, kak...@gmail.com  
> > >> wrote:
> > >> > Hi to all, i want to ask you a question, concerning the best way to do
> > >> > the following as a POST request:
> > >> > There is server-servlet that accepts xml commands
> > >> > It had the following HTTP request headers:
>
> > >> >            Host: somehost.com
> > >> >            User-Agent: Jakarta Commons-HttpClient
> > >> >            Content-Type: text/xml
> > >> >            Content-Length: 415
>
> > >> > and the following request body (reformatted here for clarity):
>
> > >> >            
> > >> >            
> > >> >              search
> > >> >            
> > >> > How can i send the above to the Listener Servlet?
> > >> > Thanks in advance
>
> > >> Use the xmlrpclib module.
>
> > > OK, sending headers with xmlrpclib,
> > > but how do i send the XML message?
>
> > Your XML message is an XML RPC message.  You will use xmlrpclib like this:
>
> > server_proxy = xmlrpclib.ServerProxy(('somehost.com', 80))
> > result = server_proxy.search()
>
> > The call to server_proxy.search will result in an actual XML RPC
> > message being sent.
>
> > Read up on the xmlrpclib documentation 
> > here:http://docs.python.org/library/xmlrpclib.html, and the XMLRPC spec
> > here:http://www.xmlrpc.com/spec
>
> > --
> > regards,
> > kushal
>
> Ok I got it!
> Thank you!!!
>
> A.K

Apparently the server i'm trying to connect accepts only POST
connections. So xmlrpclib is useless.
I think I need the httplib library module.

Any hints?

A.K.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: HTTP Post Request

2010-05-12 Thread kak...@gmail.com
On May 12, 6:13 am, Kushal Kumaran 
wrote:
> On Tue, May 11, 2010 at 3:59 PM, kak...@gmail.com  wrote:
> > On May 11, 10:56 am, "kak...@gmail.com"  wrote:
> >> On May 11, 5:06 am, Kushal Kumaran 
> >> wrote:
>
> >> > On Mon, May 10, 2010 at 8:26 PM, kak...@gmail.com  
> >> > wrote:
> >> > > On May 10, 10:22 am, Kushal Kumaran 
> >> > > wrote:
> >> > >> On Mon, May 10, 2010 at 7:30 PM, kak...@gmail.com  
> >> > >> wrote:
> >> > >> > Hi to all, i want to ask you a question, concerning the best way to 
> >> > >> > do
> >> > >> > the following as a POST request:
> >> > >> > There is server-servlet that accepts xml commands
> >> > >> > It had the following HTTP request headers:
>
> >> > >> >            Host: somehost.com
> >> > >> >            User-Agent: Jakarta Commons-HttpClient
> >> > >> >            Content-Type: text/xml
> >> > >> >            Content-Length: 415
>
> >> > >> > and the following request body (reformatted here for clarity):
>
> >> > >> >            
> >> > >> >            
> >> > >> >              search
> >> > >> >            
> >> > >> > How can i send the above to the Listener Servlet?
> >> > >> > Thanks in advance
>
> >> > >> Use the xmlrpclib module.
>
> >> > > OK, sending headers with xmlrpclib,
> >> > > but how do i send the XML message?
>
> >> > Your XML message is an XML RPC message.  You will use xmlrpclib like 
> >> > this:
>
> >> > server_proxy = xmlrpclib.ServerProxy(('somehost.com', 80))
> >> > result = server_proxy.search()
>
> >> > The call to server_proxy.search will result in an actual XML RPC
> >> > message being sent.
>
> >> > Read up on the xmlrpclib documentation 
> >> > here:http://docs.python.org/library/xmlrpclib.html, and the XMLRPC spec
> >> > here:http://www.xmlrpc.com/spec
>
> >> Ok I got it!
> >> Thank you!!!
>
> >> A.K
>
> > Apparently the server i'm trying to connect accepts only POST
> > connections. So xmlrpclib is useless.
> > I think I need the httplib library module.
>
> > Any hints?
>
> I don't understand.  xmlrpclib sends POST requests only.  Are you
> getting an exception?  If so, please post the entire stack trace.
>
> If you want to send the data "by hand", use the httplib module.  you
> can pass your XML to the HTTPConnection.request method as the "body"
> argument.
>
> --
> regards,
> kushal

Ok i found it. I sent the xml by hand with httplib module.
i use the last example of http://docs.python.org/library/httplib.html
and it worked.
Thank you very much for your response.

A.K.
-- 
http://mail.python.org/mailman/listinfo/python-list


cmd app and xml

2010-05-14 Thread kak...@gmail.com
Hi there,
i'm writing a console app using the cmd library. I also use
xml.dom.minidom to parse an xml file that i get as a response to an
HTTP Post request.
with
data = response.read()
i get the xml response from the server.
i then feed the parser with that data.
myDoc = parse(data)
but it doesn't work.
To make it work i open an xml file and the save the data to that file.
myDoc = parse('test.xml')
that worked.
But i don't want to use the local xml file?
What am i doing wrong?

Thanks in advance
A.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: cmd app and xml

2010-05-14 Thread kak...@gmail.com
On May 14, 7:10 am, Stefan Behnel  wrote:
> kak...@gmail.com, 14.05.2010 12:46:
>
> > Hi there,
> > i'm writing a console app using the cmd library. I also use
> > xml.dom.minidom to parse an xml file that i get as a response to an
> > HTTP Post request.
> > with
> > data = response.read()
> > i get the xml response from the server.
> > i then feed the parser with that data.
> > myDoc = parse(data)
> > but it doesn't work.
>
> Note that "it doesn't work" is not a very complete description of the
> actual problem.
>
> > To make it work i open an xml file and the save the data to that file.
> > myDoc = parse('test.xml')
> > that worked.
> > But i don't want to use the local xml file?
> > What am i doing wrong?
>
> Not reading the docs?
>
> http://docs.python.org/library/xml.dom.minidom.html
>
> There is a parseString() function that does what you want.
>
> Stefan

ok, i missed that function.
Thank you so much!!
Antonis
-- 
http://mail.python.org/mailman/listinfo/python-list


parsing XML

2010-05-14 Thread kak...@gmail.com
Hi to all, let's say we have the following Xml

  
17.1
6.4
  
  
15.5
7.8
  


How can i get the players name, age and height?
DOM or SAX and how

Thanks
Antonis
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: parsing XML

2010-05-14 Thread kak...@gmail.com
On May 14, 6:22 pm, Stefan Behnel  wrote:
> kak...@gmail.com, 14.05.2010 16:57:
>
> > Hi to all, let's say we have the following Xml
> > 
> >    
> >      17.1
> >      6.4
> >    
> >    
> >      15.5
> >      7.8
> >    
> > 
>
> > How can i get the players name, age and height?
>
> Here's an overly complicated solution, but I thought that an object
> oriented design would help here.
>
>    import xml.etree.ElementTree as ET
>
>    class Player(object):
>       def __init__(self, name, age, height):
>           self.name, self.age, self.height = name, age, height
>
>    attributes = ['name', 'age', 'height']
>
>    players = []
>    for _, element in ET.iterparse("teamfile.xml"):
>        if element.tag == 'player':
>            players.append(
>                Player(*[ element.get(attr) for attr in attributes ]))
>
>    for player in players:
>        print player.name, player.age, player.height
>
> Stefan

Thanks stefan!

A.K.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: parsing XML

2010-05-17 Thread kak...@gmail.com
On May 16, 10:52 am, Stefan Behnel  wrote:
> Jake b, 16.05.2010 09:40:
>
> > Check out Amara:http://www.xml3k.org/Amara/QuickRef
>
> > It looks promising. For a pythonic solution over sax / dom.
>
> > >>> Iter(doc.team.player)
> > # or
> > >>> doc.team.player[0].name
>
> Ah, right, and there's also lxml.objectify:
>
>      from lxml.objectify import parse
>
>      root = parse('teamfile.xml').getroot()
>
>      for player in root.player:
>          print(player.attrib)
>
> Prints an attribute dict for each player.
>
> Stefan

Thank you all so much!!!
And it's not for a homework.
Just trying to find out the style DOM, SAX, ElementTree etc. that fits
better to my kind of thinking.
Antonis
-- 
http://mail.python.org/mailman/listinfo/python-list


cmd with three arguments

2010-05-17 Thread kak...@gmail.com
Hi pythonistas,
While playing with the Python Standard Library, i came across "cmd".
So I'm trying to make a console application. Everything works fine, i
created many function with do_(self, line) prefix, but when i
tried to create a function with more arguments
 i can't make it work.  e.g
def do_connect(self, ip, command):

>>> connect 127.0.0.1 delete
 Are there any work arounds

Thanks in advance

Antonis
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: cmd with three arguments

2010-05-17 Thread kak...@gmail.com
On May 17, 4:12 pm, Tim Chase  wrote:
> On 05/17/2010 07:11 AM, kak...@gmail.com wrote:
>
> > While playing with the Python Standard Library, i came across "cmd".
> > So I'm trying to make a console application. Everything works fine, i
> > created many function with do_(self, line) prefix, but when i
> > tried to create a function with more arguments
> >   i can't make it work.  e.g
> > def do_connect(self, ip, command):
>
> >>>> connect 127.0.0.1 delete
> >   Are there any work arounds
>
> You simply receive all the text after the command:
>
>    class C(Cmd):
>      def do_thing(self, arguments):
>        print repr(arguments)
>
> If you want to split it, you can do it boringly:
>
>      def do_thing(self, arguments):
>        args = arguments.split()
>
> or you can let Python's standard library do some heavy-lifting
> for you:
>
>    import shlex
>    #...
>      def do_thing(self, arguments):
>        args = shlex.split(arguments)
>
> -tkc

Thanks, great answer!!!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: cmd with three arguments

2010-05-17 Thread kak...@gmail.com
On May 17, 4:34 pm, Peter Otten <__pete...@web.de> wrote:
> kak...@gmail.com wrote:
> > Hi pythonistas,
> > While playing with the Python Standard Library, i came across "cmd".
> > So I'm trying to make a console application. Everything works fine, i
> > created many function with do_(self, line) prefix, but when i
> > tried to create a function with more arguments
> >  i can't make it work.  e.g
> > def do_connect(self, ip, command):
>
> >>>> connect 127.0.0.1 delete
> >  Are there any work arounds
>
> > Thanks in advance
>
> > Antonis
>
> You have to split the user input into arguments yourself. You can do this in
> the body of the do_xxx() methods, use a decorator, or subclass cmd.Cmd.
>
> Here's a solution using a decorator:
>
> import cmd
> import inspect
> import shlex
>
> def split(f):
>     def g(self, line):
>         argvalues = shlex.split(line)
>         argnames = inspect.getargspec(f).args
>         argcount = len(argnames) - 1
>         if len(argvalues) != argcount:
>             print "Need exactly %d args" % argcount
>             return
>         return f(self, *argvalues)
>     return g
>
> class Cmd(cmd.Cmd):
>     @split
>     def do_connect(self, ip, command):
>         print "ip=%r, command=%r" % (ip, command)
>
> if __name__ == "__main__":
>     c = Cmd()
>     c.cmdloop()
>
> And here's a subclass that avoids the need for explicit @split decorations:
>
> import cmd
> import inspect
> import shlex
>
> def split(f):
>     def g(line):
>         argvalues = shlex.split(line)
>         argnames = inspect.getargspec(f).args
>         argcount = len(argnames) -1
>         if len(argvalues) != argcount:
>             print "Need exactly %d args" % argcount
>             return
>         return f(*argvalues)
>     return g
>
> class CmdBase(cmd.Cmd, object):
>     def __getattribute__(self, name):
>         attr = object.__getattribute__(self, name)
>         if name.startswith("do_"):
>             attr = split(attr)
>         return attr
>
> class Cmd(CmdBase):
>     def do_connect(self, ip, command):
>         print "ip=%r, command=%r" % (ip, command)
>
> if __name__ == "__main__":
>     c = Cmd()
>     c.cmdloop()
>
> Now you've got an idea of the general direction you can certainly come up
> with something less hackish ;)
>
> Peter

Thanks great advice!
Antonis
-- 
http://mail.python.org/mailman/listinfo/python-list


client server console app with cmd library

2010-05-19 Thread kak...@gmail.com
Hi to all,
i need some hints about a console application i' m trying.
I want to make it act as a client and as a server at a same time.
And since it is a console application i' m using cmd library.
I want something that works like asterisk. while working with my app
i want to listen for incoming requests, that they appear automatically
at my console.
Any design considerations?
Do i have to use threads?
Please i want your advice.
Any examples somewhere?

Thanks in advance!
Antonis
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: client server console app with cmd library

2010-05-21 Thread kak...@gmail.com
On May 20, 1:54 am, "kak...@gmail.com"  wrote:
> Hi to all,
> i need some hints about a console application i' m trying.
> I want to make it act as a client and as a server at a same time.
> And since it is a console application i' m using cmd library.
> I want something that works like asterisk. while working with my app
> i want to listen for incoming requests, that they appear automatically
> at my console.
> Any design considerations?
> Do i have to use threads?
> Please i want your advice.
> Any examples somewhere?
>
> Thanks in advance!
> Antonis

Hi again. I wonder is my question stupid, or what i want can't be
done?
-- 
http://mail.python.org/mailman/listinfo/python-list


asyncore loop and cmdloop problem

2010-05-25 Thread kak...@gmail.com
Hi to all,
i'm creating a command line application using asyncore and cmd. At

if __name__ == '__main__':
import socket

args = sys.argv[1:]
if not args:
print "Usage: %s querystring" % sys.argv[0]
sys.exit(0)


address = ('localhost', 0) # let the kernel give us a port
server = EchoServer(address)
ip, port = server.address # find out what port we were given

asyncore.loop()
CmdClass().cmdloop()

what i want is that the above commands asyncore.loop() and
CmdClass().cmdloop()
running at the same time. Meaning that while the application is in cmd
mode
with the cmdloop(), it is still able to listen for incoming messages?
What should i do?

thanks in advance
A.K.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: asyncore loop and cmdloop problem

2010-05-25 Thread kak...@gmail.com
On May 25, 4:55 am, Michele Simionato 
wrote:
> On May 25, 10:42 am, "kak...@gmail.com"  wrote:
>
>
>
> > Hi to all,
> > i'm creating a command line application using asyncore and cmd. At
>
> > if __name__ == '__main__':
> >     import socket
>
> >     args = sys.argv[1:]
> >     if not args:
> >         print "Usage: %s querystring" % sys.argv[0]
> >         sys.exit(0)
>
> >     address = ('localhost', 0) # let the kernel give us a port
> >     server = EchoServer(address)
> >     ip, port = server.address # find out what port we were given
>
> >     asyncore.loop()
> >     CmdClass().cmdloop()
>
> > what i want is that the above commands asyncore.loop() and
> > CmdClass().cmdloop()
> > running at the same time. Meaning that while the application is in cmd
> > mode
> > with the cmdloop(), it is still able to listen for incoming messages?
> > What should i do?
>
> > thanks in advance
> > A.K.
>
> cmd.Cmd is blocking, so the only way it to run the cmdloop in a
> separated thread. Once for fun
> I rewrote the cmd module to be non-blocking but if you want to stick
> with the standard library you need to use a thread.

Thank you so much.
Is there a way that i can find that version of cmd?

Antonis

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: asyncore loop and cmdloop problem

2010-05-25 Thread kak...@gmail.com
On May 25, 6:48 am, Giampaolo Rodolà  wrote:
> 2010/5/25 Michele Simionato :
>
> > On May 25, 12:03 pm, Giampaolo Rodolà  wrote:
> >> Too bad cmdloop() doesn't provide an argument to return immediately.
> >> Why don't you submit this patch on the bug tracker?
>
> >> --- 
> >> Giampaolohttp://code.google.com/p/pyftpdlibhttp://code.google.com/p/psutil
>
> > Because it is not a bug, cmd was designed to be blocking. It would be
> > a feature request.
>
> Sure, a feature request, but it would be nice to have anyway. =)
> The OP question shown how this can be desirable in certain circumstances..
>
> --- Giampaolohttp://code.google.com/p/pyftpdlibhttp://code.google.com/p/psutil


Could you please provide me with a simple example how to do this with
threads.
I don't know where to put the cmdloop().
Please help, i' m so confused!
Thank you
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: asyncore loop and cmdloop problem

2010-05-25 Thread kak...@gmail.com
On May 25, 5:23 pm, Michele Simionato 
wrote:
> On May 25, 2:56 pm, "kak...@gmail.com"  wrote:
>
> > Could you please provide me with a simple example how to do this with
> > threads.
> > I don't know where to put the cmdloop().
> > Please help, i' m so confused!
> > Thank you
>
> What are you trying to do? Do you really need to use the standard
> library? Likely what you want to accomplish is already implemented in
> Twisted; I remember there was something like that in their examples
> directory.

Thank you,
and sorry for the mistake i did before with my post.

Antonis
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: asyncore loop and cmdloop problem

2010-05-25 Thread kak...@gmail.com
On May 25, 5:47 pm, "kak...@gmail.com"  wrote:
> On May 25, 5:23 pm, Michele Simionato 
> wrote:
>
> > On May 25, 2:56 pm, "kak...@gmail.com"  wrote:
>
> > > Could you please provide me with a simple example how to do this with
> > > threads.
> > > I don't know where to put the cmdloop().
> > > Please help, i' m so confused!
> > > Thank you
>
> > What are you trying to do? Do you really need to use the standard
> > library? Likely what you want to accomplish is already implemented in
> > Twisted; I remember there was something like that in their examples
> > directory.
>
> Thank you,
> and sorry for the mistake i did before with my post.
>
> Antonis

hi again. i installed twisted, but since i m not familiar with it, do
you remember which example you have in mind?
What i want to accomplish is something like "asterisk". while you send
commands from the asterisk cli, at the same time
the underlying protocol sends you notifications to the console.
without exiting the application.
thanks
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: asyncore loop and cmdloop problem

2010-05-26 Thread kak...@gmail.com
On May 26, 2:03 am, exar...@twistedmatrix.com wrote:
> On 04:31 pm, kak...@gmail.com wrote:
>
>
>
> >On May 25, 5:47 pm, "kak...@gmail.com"  wrote:
> >>On May 25, 5:23 pm, Michele Simionato 
> >>wrote:
>
> >> > On May 25, 2:56 pm, "kak...@gmail.com"  wrote:
>
> >> > > Could you please provide me with a simple example how to do this
> >>with
> >> > > threads.
> >> > > I don't know where to put the cmdloop().
> >> > > Please help, i' m so confused!
> >> > > Thank you
>
> >> > What are you trying to do? Do you really need to use the standard
> >> > library? Likely what you want to accomplish is already implemented
> >>in
> >> > Twisted; I remember there was something like that in their examples
> >> > directory.
>
> >>Thank you,
> >>and sorry for the mistake i did before with my post.
>
> >>Antonis
>
> >hi again. i installed twisted, but since i m not familiar with it, do
> >you remember which example you have in mind?
> >What i want to accomplish is something like "asterisk". while you send
> >commands from the asterisk cli, at the same time
> >the underlying protocol sends you notifications to the console.
> >without exiting the application.
> >thanks
>
> You can find a couple simple examples here:
>
>  http://twistedmatrix.com/documents/current/core/examples/
>
> Search for "stdin" and "stdio".
>
> Jean-Paul

Thank you so much, i'll check them!!!
Antonis
-- 
http://mail.python.org/mailman/listinfo/python-list


Sockets and xml problem

2010-05-28 Thread kak...@gmail.com
Hi in the following code

class MyClientHandler(SocketServer.BaseRequestHandler):
def handle(self):
print self.client_address, now( )
time.sleep(5)
while True:
xmltxt = self.request.recv(1024)<--is this ok -
enough?
if not xmltxt: break

doc = minidom.parseString(data)
 <--- it also fails for parse(data)
rootNode = doc.documentElement

level = 0

walk(rootNode, outFile, level)
  <---just a function to print the xml
self.request.send('Echo=>%s at %s' % (data, now( )))
rootNode = doc.documentElement
level = 0
walk(rootNode, outFile, level)
self.request.send('Echo=>%s at %s' % (data, now( )))
self.request.close( )

# make a threaded server, listen/handle clients forever
myaddr = (myHost, myPort)
server = SocketServer.ThreadingTCPServer(myaddr, MyClientHandler)
server.serve_forever( )


I want to send XML messages from my client. The server sends back the
XML it receives but
the parser exits with error codes.
What am i doing wrong.

Thanks in advance
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sockets and xml problem

2010-05-28 Thread kak...@gmail.com
On May 28, 3:23 pm, Stefan Behnel  wrote:
> kak...@gmail.com, 28.05.2010 13:50:
>
> > Hi in the following code
>
> > class MyClientHandler(SocketServer.BaseRequestHandler):
> >      def handle(self):
> >          print self.client_address, now( )
> >          time.sleep(5)
> >          while True:
> >              xmltxt = self.request.recv(1024)<--is this ok -
> > enough?
>
> Depends. If your messages are never larger than 1K, this is enough.
> Otherwise, you have to collect the data, instead of parsing each chunk
> separately.
>
> I suggest using the incremental parser in xml.etree.ElementTree, which
> allows you to push more data into the parser as it comes in. When done,
> call it's .close() method to retrieve the result.
>
> http://docs.python.org/library/xml.etree.elementtree.html#xmltreebuil...
>
> > I want to send XML messages from my client. The server sends back the
> > XML it receives but the parser exits with error codes.
>
> You should also rethink your approach one more time. Are you sure that a
> raw socket is a good protocol for sending your messages? In many cases, a
> proper higher-level transport protocol like HTTP is much better suited. If
> you provide more details about what you are trying to do, others may be
> able to help you further.
>
> Stefan

Stefan first of all thank you for your response.
I don't want anything fancy. Just a simple server that accepts xml
messages from multple clients in xml,
parses the XML and show it in a console.
Antonis
-- 
http://mail.python.org/mailman/listinfo/python-list


Omit the headers from XML message

2010-05-28 Thread kak...@gmail.com
Hi i have the following xml message i want to omit the headers, any
hints?

POST /test/pcp/Listener HTTP/1.1
User-Agent: Jakarta Commons-HttpClient/3.1
Host: 127.0.0.1:50002
Content-Length: 547

http://demo.com/demo";>
  

  
scvdcvsdv
sdfv
Antonis Kaklis
away
testing the new client
jlkdjf
android
  

  

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Omit the headers from XML message

2010-05-28 Thread kak...@gmail.com
On 28 Μάϊος, 18:45, Jon Clements  wrote:
> On 28 May, 16:24, "kak...@gmail.com"  wrote:
>
>
>
>
>
> > Hi i have the following xml message i want to omit the headers, any
> > hints?
>
> > POST /test/pcp/Listener HTTP/1.1
> > User-Agent: Jakarta Commons-HttpClient/3.1
> > Host: 127.0.0.1:50002
> > Content-Length: 547
>
> > http://demo.com/demo";>
> >   
> >     
> >       
> >         scvdcvsdv
> >         sdfv
> >         Antonis Kaklis
> >         away
> >         testing the new client
> >         jlkdjf
> >         android
> >       
> >     
> >   
> > 
>
> Assuming the header is separated by a blank line, something like:
>
> list(islice(dropwhile(bool, s.split('\n')), 1, None))

Thank you!!!

Antonis
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Omit the headers from XML message

2010-05-28 Thread kak...@gmail.com
On May 28, 7:48 pm, Peter Otten <__pete...@web.de> wrote:
> Jon Clements wrote:
> > On 28 May, 16:24, "kak...@gmail.com"  wrote:
> >> Hi i have the following xml message i want to omit the headers, any
> >> hints?
> > Assuming the header is separated by a blank line, something like:
>
> > list(islice(dropwhile(bool, s.split('\n')), 1, None))
>
> Making the same assumptions, but giving a single string instead of a list of
> lines:
>
> s.partition("\n\n")[-1]
>
> Peter

Thank you all for your responses!

Antonis
-- 
http://mail.python.org/mailman/listinfo/python-list


Threads with Cmd and socket server combination

2010-05-30 Thread kak...@gmail.com
hi,
I have implement a command line app using Python's cmd library module
and it works fine.
I 've also create a simple threaded socket server. How can i merge the
two ones, so that the
console app, is also a listening server? How can i achieve that with
threads?
I'm trying for days and i can't make it work! I know it can be done
with Twisted but i want
 to understand how thread works.

The console:

class BM(CmdBase):
"""Simple custom command processor"""

def do_acmd(self):
pass

if __name__ == '__main__':
BM().cmdloop()

and the Server:

class MyClientHandler(SocketServer.BaseRequestHandler):
def handle(self):
pass


server = SocketServer.ThreadingTCPServer(myaddr, MyClientHandler)
server.serve_forever( )

Thanks in advance
Threads can be so difficult
A.K.
-- 
http://mail.python.org/mailman/listinfo/python-list


expat parsing error

2010-06-01 Thread kak...@gmail.com
Hi i'm doing the following:

def start_element(name, attrs):
print 'Start element:', name, attrs
def end_element(name):
print 'End element:', name
def char_data(data):
print 'Character data:', repr(data)

class SimpleServer(LineReceiver): # Using Twisted

def connectionMade(self):
print 'Connection from: ', self.transport.client

def connectionLost(self, reason):
print self.transport.client, 'Disconnected'

def dataReceived(self, line):
"""Here the XML Parser"""

p = xml.parsers.expat.ParserCreate()

p.StartElementHandler = start_element
p.EndElementHandler = end_element
p.CharacterDataHandler = char_data
p.Parse(line, 1)

I got the following error
---  ---
  File "/usr/lib/python2.6/site-packages/Twisted-10.0.0-py2.6-linux-
x86_64.egg/twisted/internet/selectreactor.py", line 146, in
_doReadOrWrite
why = getattr(selectable, method)()
  File "/usr/lib/python2.6/site-packages/Twisted-10.0.0-py2.6-linux-
x86_64.egg/twisted/internet/tcp.py", line 460, in doRead
return self.protocol.dataReceived(data)
  File "stdiodemo.py", line 419, in dataReceived
p.Parse(line, 1)
xml.parsers.expat.ExpatError: syntax error: line 1, column 0


The XML Message is coming in the form of:

POST /test/pcp/Listener HTTP/1.1
user-agent:hjahsjdhaskd asdja d
Host:127.0.0.1
Content-Length: 547

http://bytemobile.com/pttv";>
  
200
OK, found 5 session entries

  
06d4d59bf8f1abf57cadfe10139dd874
82
android
  

  


Please give me some hints
-- 
http://mail.python.org/mailman/listinfo/python-list


expat parsing error

2010-06-01 Thread kak...@gmail.com
Hi i'm doing the following:

def start_element(name, attrs):
print 'Start element:', name, attrs
def end_element(name):
print 'End element:', name
def char_data(data):
print 'Character data:', repr(data)

class SimpleServer(LineReceiver): # Using Twisted

def connectionMade(self):
print 'Connection from: ', self.transport.client

def connectionLost(self, reason):
print self.transport.client, 'Disconnected'

def dataReceived(self, line):
"""Here the XML Parser"""

p = xml.parsers.expat.ParserCreate()

p.StartElementHandler = start_element
p.EndElementHandler = end_element
p.CharacterDataHandler = char_data
p.Parse(line, 1)

I got the following error
---  ---
  File "/usr/lib/python2.6/site-packages/Twisted-10.0.0-py2.6-linux-
x86_64.egg/twisted/internet/selectreactor.py", line 146, in
_doReadOrWrite
why = getattr(selectable, method)()
  File "/usr/lib/python2.6/site-packages/Twisted-10.0.0-py2.6-linux-
x86_64.egg/twisted/internet/tcp.py", line 460, in doRead
return self.protocol.dataReceived(data)
  File "stdiodemo.py", line 419, in dataReceived
p.Parse(line, 1)
xml.parsers.expat.ExpatError: syntax error: line 1, column 0

The XML Message is coming in the form of:

POST /test/pcp/Listener HTTP/1.1
user-agent:hjahs
Host:127.0.0.1
Content-Length: 547

http://1270.0.01/pttv";>
  
200
OK, found 5 session entries

  
06d4d59bfdfe10139dd874
82
and
  

  


Please give me some hints
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: expat parsing error

2010-06-01 Thread kak...@gmail.com
On Jun 1, 9:51 am, John Bokma  wrote:
> "kak...@gmail.com"  writes:
> > I got the following error
> > ---  ---
> >   File "/usr/lib/python2.6/site-packages/Twisted-10.0.0-py2.6-linux-
> > x86_64.egg/twisted/internet/selectreactor.py", line 146, in
> > _doReadOrWrite
> >     why = getattr(selectable, method)()
> >   File "/usr/lib/python2.6/site-packages/Twisted-10.0.0-py2.6-linux-
> > x86_64.egg/twisted/internet/tcp.py", line 460, in doRead
> >     return self.protocol.dataReceived(data)
> >   File "stdiodemo.py", line 419, in dataReceived
> >     p.Parse(line, 1)
> > xml.parsers.expat.ExpatError: syntax error: line 1, column 0
>
> > The XML Message is coming in the form of:
>
> > POST /test/pcp/Listener HTTP/1.1
>
> Does Expat get this line as well? If so, that's the reason why you get
> an error at line 1, column 0.
>
> --
> John Bokma                                                               j3b
>
> Hacking & Hiking in Mexico -  http://johnbokma.com/http://castleamber.com/- 
> Perl & Python Development

Yes but how can i fix it, how to "ignore" the headers and parse only
the XML?
Thanks
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: expat parsing error

2010-06-01 Thread kak...@gmail.com
On Jun 1, 10:34 am, Stefan Behnel  wrote:
> kak...@gmail.com, 01.06.2010 16:00:
>
> > how can i fix it, how to "ignore" the headers and parse only
> > the XML?
>
> Consider reading the answers you got in the last thread that you opened
> with exactly this question.
>
> Stefan

That's exactly, what i did but something seems to not working with the
solutions i had, when i changed my implementation from pure Python's
sockets to twisted library!
That's the reason i have created a new post!
Any ideas why this happened?
Thanks Stefan

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: expat parsing error

2010-06-01 Thread kak...@gmail.com
On Jun 1, 11:09 am, John Bokma  wrote:
> "kak...@gmail.com"  writes:
> > On Jun 1, 10:34 am, Stefan Behnel  wrote:
> >> kak...@gmail.com, 01.06.2010 16:00:
>
> >> > how can i fix it, how to "ignore" the headers and parse only
> >> > the XML?
>
> >> Consider reading the answers you got in the last thread that you opened
> >> with exactly this question.
>
> >> Stefan
>
> > That's exactly, what i did but something seems to not working with the
> > solutions i had, when i changed my implementation from pure Python's
> > sockets to twisted library!
> > That's the reason i have created a new post!
> > Any ideas why this happened?
>
> As I already explained: if you send your headers as well to any XML
> parser it will choke on those, because the headers are /not/ valid /
> well-formed XML. The solution is to remove the headers from your
> data. As I explained before: headers are followed by one empty
> line. Just remove lines up and until including the empty line, and pass
> the data to any XML parser.
>
> --
> John Bokma                                                               j3b
>
> Hacking & Hiking in Mexico -  http://johnbokma.com/http://castleamber.com/- 
> Perl & Python Development

Thank you so much i'll try it!
Antonis
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: expat parsing error

2010-06-01 Thread kak...@gmail.com
On Jun 1, 11:12 am, "kak...@gmail.com"  wrote:
> On Jun 1, 11:09 am, John Bokma  wrote:
>
>
>
> > "kak...@gmail.com"  writes:
> > > On Jun 1, 10:34 am, Stefan Behnel  wrote:
> > >> kak...@gmail.com, 01.06.2010 16:00:
>
> > >> > how can i fix it, how to "ignore" the headers and parse only
> > >> > the XML?
>
> > >> Consider reading the answers you got in the last thread that you opened
> > >> with exactly this question.
>
> > >> Stefan
>
> > > That's exactly, what i did but something seems to not working with the
> > > solutions i had, when i changed my implementation from pure Python's
> > > sockets to twisted library!
> > > That's the reason i have created a new post!
> > > Any ideas why this happened?
>
> > As I already explained: if you send your headers as well to any XML
> > parser it will choke on those, because the headers are /not/ valid /
> > well-formed XML. The solution is to remove the headers from your
> > data. As I explained before: headers are followed by one empty
> > line. Just remove lines up and until including the empty line, and pass
> > the data to any XML parser.
>
> > --
> > John Bokma                                                               j3b
>
> > Hacking & Hiking in Mexico -  
> > http://johnbokma.com/http://castleamber.com/-Perl & Python Development
>
> Thank you so much i'll try it!
> Antonis

Dear John can you provide me a simple working solution?
I don't seem to get it
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: expat parsing error

2010-06-02 Thread kak...@gmail.com
On 2 Ιούν, 03:47, John Machin  wrote:
> On Jun 2, 1:57 am, "kak...@gmail.com"  wrote:
>
>
>
>
>
> > On Jun 1, 11:12 am, "kak...@gmail.com"  wrote:
>
> > > On Jun 1, 11:09 am, John Bokma  wrote:
>
> > > > "kak...@gmail.com"  writes:
> > > > > On Jun 1, 10:34 am, Stefan Behnel  wrote:
> > > > >> kak...@gmail.com, 01.06.2010 16:00:
>
> > > > >> > how can i fix it, how to "ignore" the headers and parse only
> > > > >> > the XML?
>
> > > > >> Consider reading the answers you got in the last thread that you 
> > > > >> opened
> > > > >> with exactly this question.
>
> > > > >> Stefan
>
> > > > > That's exactly, what i did but something seems to not working with the
> > > > > solutions i had, when i changed my implementation from pure Python's
> > > > > sockets to twisted library!
> > > > > That's the reason i have created a new post!
> > > > > Any ideas why this happened?
>
> > > > As I already explained: if you send your headers as well to any XML
> > > > parser it will choke on those, because the headers are /not/ valid /
> > > > well-formed XML. The solution is to remove the headers from your
> > > > data. As I explained before: headers are followed by one empty
> > > > line. Just remove lines up and until including the empty line, and pass
> > > > the data to any XML parser.
>
> > > > --
> > > > John Bokma                                                              
> > > >  j3b
>
> > > > Hacking & Hiking in Mexico -  
> > > > http://johnbokma.com/http://castleamber.com/-Perl&Python Development
>
> > > Thank you so much i'll try it!
> > > Antonis
>
> > Dear John can you provide me a simple working solution?
> > I don't seem to get it
>
> You're not wrong. Trysomething like this:
>
> rubbish1, rubbish2, xml = your_guff.partition('\n\n')

Ok thanks a lot!
Antonis
-- 
http://mail.python.org/mailman/listinfo/python-list


Load/Performance Testing of a Web Server

2010-07-09 Thread kak...@gmail.com
Hi to all, i want to stress test   a tomcat web server, so that i
could find out its limits. e.g how many users can be connected and
request a resource concurrently.
I used JMeter which is an excellent tool, but i would like to use a
more pythonic approach.
Any hints

Antonis
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Load/Performance Testing of a Web Server

2010-07-11 Thread kak...@gmail.com
On Jul 9, 4:44 pm, Simon Brunning  wrote:
> On 9 July 2010 14:17, kak...@gmail.com  wrote:
>
> > Hi to all, i want to stress test   a tomcat web server, so that i
> > could find out its limits. e.g how many users can be connected and
> > request a resource concurrently.
> > I used JMeter which is an excellent tool, but i would like to use a
> > more pythonic approach.
>
> The Grinder?
>
> --
> Cheers,
> Simon B.

Thank you Simon!
I'll give it a try.
It looks very promising.

Antonis K.
-- 
http://mail.python.org/mailman/listinfo/python-list


stdiodemo (twisted-python) and enable command history

2010-07-14 Thread kak...@gmail.com
Hello again to all,
While playing and extending stdiodemo.py,
a came up with a thought of adding command line history.
Is this possible?
Any hints?

Thanks

Antonis K.
-- 
http://mail.python.org/mailman/listinfo/python-list


linux console command line history

2010-07-20 Thread kak...@gmail.com
Hi to all,
I 'm writing a linux console app with sockets. It's basically a client
app that fires commands in a server.
For example:
$log user 55
$sessions list
$server list etc.
What i want is, after entering some commands, to press the up arrow
key and see the previous commands that i have executed.
Any hints? Any examples?

Antonis
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: linux console command line history

2010-07-20 Thread kak...@gmail.com
On Jul 21, 12:47 am, Benjamin Kaplan  wrote:
> On Tue, Jul 20, 2010 at 2:38 PM, kak...@gmail.com  wrote:
> > Hi to all,
> > I 'm writing a linux console app with sockets. It's basically a client
> > app that fires commands in a server.
> > For example:
> > $log user 55
> > $sessions list
> > $server list etc.
> > What i want is, after entering some commands, to press the up arrow
> > key and see the previous commands that i have executed.
> > Any hints? Any examples?
>
> > Antonis
> > --
>
> Look at the readline module.http://docs.python.org/library/readline.html

ok that's fine, thanks.
I have also find a very helpful example in PyMoTW
http://www.doughellmann.com/PyMOTW/readline/index.html(Thanks
Doug!!!).
But if i want to run this in it's own separate thread, how could i do
that?
there is an
# Prompt the user for text
input_loop()

which is blocking?



Antonis K.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: linux console command line history

2010-07-21 Thread kak...@gmail.com
On Jul 21, 9:03 am, Michele Simionato 
wrote:
> On Jul 20, 11:38 pm, "kak...@gmail.com"  wrote:
>
> > Hi to all,
> > I 'm writing a linux console app with sockets. It's basically a client
> > app that fires commands in a server.
> > For example:
> > $log user 55
> > $sessions list
> > $server list etc.
> > What i want is, after entering some commands, to press the up arrow
> > key and see the previous commands that i have executed.
> > Any hints? Any examples?
>
> > Antonis
>
> You may find interesting to look at the source code for plac (http://
> micheles.googlecode.com/hg/plac/doc/plac_adv.html). The readline
> support (including command history and autocompletion) is implemented
> in the ReadlineInput class 
> (seehttp://code.google.com/p/micheles/source/browse/plac/plac_ext.py).
> If you just want command history you can use rlwrap (http://
> freshmeat.net/projects/rlwrap).

That's great! thank you so much Michele!

Antonis
-- 
http://mail.python.org/mailman/listinfo/python-list


Sorting a list created from a parsed xml message

2010-07-21 Thread kak...@gmail.com
Hi pythonistas,
>From the subject of my message it's clear that i get an xml message
from a socket, i parse it and the result is a list like the one that
follows:
ID_Col
4 Serverak  ip  OFFLINE

29  Server  and2ip  OFFLINE

5 Proxy l34e ip OFFLINE

6 Proxy barcip  ONLINE

41Proxy proxy-2 ip  ONLINE

53Serverserver-4ip  ONLINE

52Serverserver-3ip  ONLINE


What i want is to print this list sorted by ID_Col?
Any Suggestions?

Antonis K.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sorting a list created from a parsed xml message

2010-07-21 Thread kak...@gmail.com
On Jul 21, 8:58 am, Stefan Behnel  wrote:
> kak...@gmail.com, 21.07.2010 14:36:
>
> > From the subject of my message it's clear that i get an xml message
> > from a socket,
>
> Not at all, but now that you say it...
>
>
>
> > i parse it and the result is a list like the one that
> > follows:
> > ID_Col
> > 4    Server        ak              ip      OFFLINE
>
> > 29      Server     and2    ip      OFFLINE
>
> > 5    Proxy         l34e         ip OFFLINE
>
> > 6            Proxy         barc            ip      ONLINE
>
> > 41           Proxy         proxy-2         ip      ONLINE
>
> > 53           Server        server-4        ip      ONLINE
>
> > 52           Server        server-3        ip      ONLINE
>
> Doesn't look like a Python list to me...
>
> > What i want is to print this list sorted by ID_Col?
> > Any Suggestions?
>
> Assuming that the above is supposed to represent a list of tuples, you can
> use the .sort() method on the list and pass operator.itemgetter(0) as 'key'
> argument (see the sort() method and the operator module).
>
> Stefan

No it is not a Python list at all. This the way i print the parsed
items 'like a list'.
But i want them to be sorted.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sorting a list created from a parsed xml message

2010-07-21 Thread kak...@gmail.com
On Jul 21, 9:04 am, "kak...@gmail.com"  wrote:
> On Jul 21, 8:58 am, Stefan Behnel  wrote:
>
>
>
> > kak...@gmail.com, 21.07.2010 14:36:
>
> > > From the subject of my message it's clear that i get an xml message
> > > from a socket,
>
> > Not at all, but now that you say it...
>
> > > i parse it and the result is a list like the one that
> > > follows:
> > > ID_Col
> > > 4    Server        ak              ip      OFFLINE
>
> > > 29      Server     and2    ip      OFFLINE
>
> > > 5    Proxy         l34e         ip OFFLINE
>
> > > 6            Proxy         barc            ip      ONLINE
>
> > > 41           Proxy         proxy-2         ip      ONLINE
>
> > > 53           Server        server-4        ip      ONLINE
>
> > > 52           Server        server-3        ip      ONLINE
>
> > Doesn't look like a Python list to me...
>
> > > What i want is to print this list sorted by ID_Col?
> > > Any Suggestions?
>
> > Assuming that the above is supposed to represent a list of tuples, you can
> > use the .sort() method on the list and pass operator.itemgetter(0) as 'key'
> > argument (see the sort() method and the operator module).
>
> > Stefan
>
> No it is not a Python list at all. This the way i print the parsed
> items 'like a list'.
> But i want them to be sorted.

Well i did this:

SortedServers = []

for session in sessions:
for IP in session.getElementsByTagName("ipAddress"):
 for iphn in session.getElementsByTagName("hostName"):
  tempTuple = session.getAttribute("id"),
session.getAttribute("type"), iphn.childNodes[0].data,
IP.childNodes[0].data, session.getAttribute("status")

  SortedServers.append(tempTuple)

Sorted = sorted(SortedServers, key=lambda id: SortedServers[0])
for item in Sorted:
 print item

but the list is still unsorted and with u' in front of each item

(u'4', u'Server', u'aika74', u'ip', u'OFFLINE')
(u'29', u'Server', u'ando', u'ip2', u'OFFLINE')

How do i remove the u'

Antonis
-- 
http://mail.python.org/mailman/listinfo/python-list


Convert Unix timestamp to Readable Date/time

2010-07-22 Thread kak...@gmail.com
Well i have the following number 1279796174846
 i did the following:

mdate = 1279796174846
tempStr = str(mdate)
tempStr2 = tempStr[:-3]
tempInt = int(tempStr2)
print "Last Login :", datetime.datetime.fromtimestamp(tempInt)

that prints out: 2010-07-22 06:56:14
But when i check my answer at http://www.onlineconversion.com/unix_time.htm

i got:Thu, 22 Jul 2010 10:56:14 GMT which is what i want.
What am i doing wrong?
Thanks

Antonis
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sorting a list created from a parsed xml message

2010-07-27 Thread kak...@gmail.com
On Jul 22, 12:56 pm, Thomas Jollans  wrote:
> On 07/21/2010 03:38 PM, kak...@gmail.com wrote:
>
>
>
> > On Jul 21, 9:04 am, "kak...@gmail.com"  wrote:
> >> On Jul 21, 8:58 am, Stefan Behnel  wrote:
>
> >>> kak...@gmail.com, 21.07.2010 14:36:
>
> >>>> From the subject of my message it's clear that i get an xml message
> >>>> from a socket,
>
> >>> Not at all, but now that you say it...
>
> >>>> i parse it and the result is a list like the one that
> >>>> follows:
> >>>> ID_Col
> >>>> 4    Server        ak              ip      OFFLINE
>
> >>>> 29      Server     and2    ip      OFFLINE
>
> >>>> 5    Proxy         l34e         ip OFFLINE
>
> >>>> 6            Proxy         barc            ip      ONLINE
>
> >>>> 41           Proxy         proxy-2         ip      ONLINE
>
> >>>> 53           Server        server-4        ip      ONLINE
>
> >>>> 52           Server        server-3        ip      ONLINE
>
> >>> Doesn't look like a Python list to me...
>
> >>>> What i want is to print this list sorted by ID_Col?
> >>>> Any Suggestions?
>
> >>> Assuming that the above is supposed to represent a list of tuples, you can
> >>> use the .sort() method on the list and pass operator.itemgetter(0) as 
> >>> 'key'
> >>> argument (see the sort() method and the operator module).
>
> >>> Stefan
>
> >> No it is not a Python list at all. This the way i print the parsed
> >> items 'like a list'.
> >> But i want them to be sorted.
>
> > Well i did this:
>
> > SortedServers = []
>
> > for session in sessions:
> >     for IP in session.getElementsByTagName("ipAddress"):
> >          for iphn in session.getElementsByTagName("hostName"):
> >               tempTuple = session.getAttribute("id"),
> > session.getAttribute("type"), iphn.childNodes[0].data,
> > IP.childNodes[0].data, session.getAttribute("status")
>
> Please try to persuade your mail client to not mess up python code, if
> you could. It would make this *so* much easier to read
>
>
>
> >               SortedServers.append(tempTuple)
>
> > Sorted = sorted(SortedServers, key=lambda id: SortedServers[0])
>
> Anyway, let's look at that key function of yours:
>
> key=lambda id: SortedServers[0]
>
> translated to traditional function syntax:
>
> def key(id):
>     return SortedServers[0]
>
> No matter which item sorted() examines, the key it sorts by is always
> the same (the first item of the WHOLE LIST).
> You want something more like this:
>
> def key(row):
>     return row[0]
>
> ergo, what you want, all in all, is either of these:
>
> Sorted = sorted(SortedServers, key=(lambda row: row[0])) # option 1
> SortedServers.sort(key=(lambda row: row[0]))             # option 2
>
> option 2, the in-place sort, might be faster.
>
> (and, as Stefan noted, as you probably want a numeric sort, you'll want
> your key to be an int)
>
> > for item in Sorted:
> >      print item
>
> > but the list is still unsorted and with u' in front of each item
>
> > (u'4', u'Server', u'aika74', u'ip', u'OFFLINE')
> > (u'29', u'Server', u'ando', u'ip2', u'OFFLINE')
>
> > How do i remove the u'
>
> > Antonis
>
>

Thank you so much for your detailed response!

Antonis K.
-- 
http://mail.python.org/mailman/listinfo/python-list


parsing different xml messages

2010-07-27 Thread kak...@gmail.com
Hello,
I receive the following different Xml Messages from a socket:

http://test.com/pt";>

  



  

5a62ded

101

Angie

online



Some IP





  




http://test.com/pt";>

  



  Server server-1 is going down for redeploy!



  



Which is the best way to make a distinction between them so that every
time my app receives the one or the other, parse them correctly?

Thanks

Antonis K.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: parsing different xml messages

2010-07-27 Thread kak...@gmail.com
On Jul 27, 6:30 am, Stefan Behnel  wrote:
> kak...@gmail.com, 27.07.2010 12:17:
>
> > I receive the following different Xml Messages from a socket:
>
>  From a bare socket? TCP? UDP? Or what else?
>
> > Which is the best way to make a distinction between them so that every
> > time my app receives the one or the other, parse them correctly?
>
> Use an application level protocol?
>
> Stefan

>From a tcp socket using the twisted framework.
Application level protocol... Such as?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: parsing different xml messages

2010-07-27 Thread kak...@gmail.com
On Jul 27, 8:14 am, Stefan Behnel  wrote:
> kak...@gmail.com, 27.07.2010 13:58:
>
>
>
> > On Jul 27, 6:30 am, Stefan Behnel wrote:
> >> kak...@gmail.com, 27.07.2010 12:17:
>
> >>> I receive the following different Xml Messages from a socket:
>
> >>   From a bare socket? TCP? UDP? Or what else?
>
> >>> Which is the best way to make a distinction between them so that every
> >>> time my app receives the one or the other, parse them correctly?
>
> >> Use an application level protocol?
>
> >> Stefan
>
> >> From a tcp socket using the twisted framework.
> >> Application level protocol... Such as?
>
> Depends on what you *can* use. Do you control the sending side?
>
> Note: giving better details helps others in giving better answers.
>
> Stefan

Well yes you are right!
I can't control the sending side.
The app i'm writing just accepts incoming xml messages. Like the ones
above.
When i receive a message I parse it and print the information.
I know how to parse both xml messages.
What i want is to distinguish them so that i can trigger the
appropriate parsing method.


A.K.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: parsing different xml messages

2010-07-27 Thread kak...@gmail.com
On Jul 27, 8:41 am, Stefan Behnel  wrote:
> kak...@gmail.com, 27.07.2010 14:26:
>
>
>
> > On Jul 27, 8:14 am, Stefan Behnel wrote:
> >> kak...@gmail.com, 27.07.2010 13:58:
>
> >>> On Jul 27, 6:30 am, Stefan Behnel wrote:
> >>>> kak...@gmail.com, 27.07.2010 12:17:
>
> >>>>> I receive the following different Xml Messages from a socket:
>
> >>>>    From a bare socket? TCP? UDP? Or what else?
>
> >>>>> Which is the best way to make a distinction between them so that every
> >>>>> time my app receives the one or the other, parse them correctly?
>
> >>>> Use an application level protocol?
>
> >>>>  From a tcp socket using the twisted framework.
> >>>> Application level protocol... Such as?
>
> >> Depends on what you *can* use. Do you control the sending side?
>
> >> Note: giving better details helps others in giving better answers.
>
> > Well yes you are right!
> > I can't control the sending side.
> > The app i'm writing just accepts incoming xml messages. Like the ones
> > above.
> > When i receive a message I parse it and print the information.
> > I know how to parse both xml messages.
> > What i want is to distinguish them so that i can trigger the
> > appropriate parsing method.
>
> Do they come in concatenated or one per connection?
>
> Stefan

one per connection.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: parsing different xml messages

2010-07-27 Thread kak...@gmail.com
On Jul 27, 9:06 am, Stefan Behnel  wrote:
> kak...@gmail.com, 27.07.2010 14:43:
>
>
>
> > On Jul 27, 8:41 am, Stefan Behnel wrote:
> >> kak...@gmail.com, 27.07.2010 14:26:
>
> >>> On Jul 27, 8:14 am, Stefan Behnel wrote:
> >>>> kak...@gmail.com, 27.07.2010 13:58:
>
> >>>>> On Jul 27, 6:30 am, Stefan Behnel wrote:
> >>>>>> kak...@gmail.com, 27.07.2010 12:17:
>
> >>>>>>> I receive the following different Xml Messages from a socket:
>
> >>>>>>     From a bare socket? TCP? UDP? Or what else?
>
> >>>>>>> Which is the best way to make a distinction between them so that every
> >>>>>>> time my app receives the one or the other, parse them correctly?
>
> >>>>>> Use an application level protocol?
>
> >>>>>>   From a tcp socket using the twisted framework.
> >>>>>> Application level protocol... Such as?
>
> >>>> Depends on what you *can* use. Do you control the sending side?
>
> >>>> Note: giving better details helps others in giving better answers.
>
> >>> Well yes you are right!
> >>> I can't control the sending side.
> >>> The app i'm writing just accepts incoming xml messages. Like the ones
> >>> above.
> >>> When i receive a message I parse it and print the information.
> >>> I know how to parse both xml messages.
> >>> What i want is to distinguish them so that i can trigger the
> >>> appropriate parsing method.
>
> >> Do they come in concatenated or one per connection?
>
> >> Stefan
>
> > one per connection.
>
> Ah, ok, then just parse the message using (c)ElementTree and look at the
> name of the first child below the root node (assuming that both messages
> were supposed to have the same root node, as you may have wanted to
> indicate in your original posting). A dict dispatch to a function or method
> will do just fine.
>
> Stefan

ok that's great, thanks Stefan
i'll try it.

Antonis
-- 
http://mail.python.org/mailman/listinfo/python-list