questions to anyone who uses wxPython

2006-07-19 Thread damacy
hello. i'm using wxPython as my GUI package and whenever my program
executes a long process which takes at least 2 or 3 seconds, the user
interface gets corrupted while executing the progrocess during the
period.

i have tried the following lines of code...

frame = mainwindow(None, -1, 'my program')
...
...
frame.UpdateWindowUI()

and it did not make any difference at all. 

could anyone help me?

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


exception handling; python program that interacts with postgresql db

2006-08-02 Thread damacy
hi, there. i have this question which might sound quite stupid to some
people, but here we go anyway.

i have written a python program which interacts with a postgresql
database. what it does is simply drops an existing database called
'mytempdb'.

the code looks like below;

link = subprocess.Popen(command, stdin = subprocess.PIPE, stdout =
subprocess.PIPE, shell = True)
link.communicate(password)
link.wait()

where command looks like "psql -h 127.0.0.1 -U postgres -W -f filename"
and
filename is the name of the file which contains a single SQL command
which is "drop database mytempdb".

the program works fine as long as a correct password is supplied,
however, i have a problem if the password is incorrect since this
exception is *not* handled within the scope of my program, instead,
what is does is showing some error messages in the prompt. so my
program, without knowing whether an errors has taken place or not, goes
on to execute the next task.

any clue? please let me know if you think the problem is not well
addressed. =)

thanks. have a nice one.

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


Re: exception handling; python program that interacts with postgresql db

2006-08-02 Thread damacy

hiaips wrote:
> damacy wrote:
> > hi, there. i have this question which might sound quite stupid to some
> > people, but here we go anyway.
> >
> > i have written a python program which interacts with a postgresql
> > database. what it does is simply drops an existing database called
> > 'mytempdb'.
> >
> > the code looks like below;
> >
> > link = subprocess.Popen(command, stdin = subprocess.PIPE, stdout =
> > subprocess.PIPE, shell = True)
> > link.communicate(password)
> > link.wait()
> >
> > where command looks like "psql -h 127.0.0.1 -U postgres -W -f filename"
> > and
> > filename is the name of the file which contains a single SQL command
> > which is "drop database mytempdb".
> >
> > the program works fine as long as a correct password is supplied,
> > however, i have a problem if the password is incorrect since this
> > exception is *not* handled within the scope of my program, instead,
> > what is does is showing some error messages in the prompt. so my
> > program, without knowing whether an errors has taken place or not, goes
> > on to execute the next task.
> >
> > any clue? please let me know if you think the problem is not well
> > addressed. =)
> >
> > thanks. have a nice one.
>
> Hi, damacy,
>
> Maybe I'm not understanding your code 100%, but have you tried catching
> the return value of the psql process that you're launching? Just a
> thought...
>
> --hiaips

hi, hiaips. thanks for your reply.

are you talking about a try-except block? yes, i used that in case the
psql process might throw an exception if there is any. but
unfortunately, it does not do so.

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


Re: exception handling; python program that interacts with postgresql db

2006-08-02 Thread damacy
yes, i'll have a read. thanks. =)


hiaips wrote:
> Another option would be to use the psycopg module to connect to
> postgres from within your Python code. See
> http://www.initd.org/projects/psycopg1 for more information.
> 
> --hiaips

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


Re: exception handling; python program that interacts with postgresql db

2006-08-21 Thread damacy
thanks. i started to use psycopg.

however, i have this error message and i don't quite get what it means.

it says "DROP DATABASE cannot run inside a transaction block".

does anyone have a clue?

Tim Roberts wrote:
> "damacy" <[EMAIL PROTECTED]> wrote:
>
> >hi, there. i have this question which might sound quite stupid to some
> >people, but here we go anyway.
> >
> >i have written a python program which interacts with a postgresql
> >database. what it does is simply drops an existing database called
> >'mytempdb'.
> >
> >the code looks like below;
> >
> >link = subprocess.Popen(command, stdin = subprocess.PIPE, stdout =
> >subprocess.PIPE, shell = True)
> >link.communicate(password)
> >link.wait()
> >
> >where command looks like "psql -h 127.0.0.1 -U postgres -W -f filename"
> >and
> >filename is the name of the file which contains a single SQL command
> >which is "drop database mytempdb".
>
> hiaips is right.  The right way to do this is to use a Postgres module.
> psycopg is my favorite, but there are several alternatives.
>
> import psycopg
> db = psycopg.connect(
> "dbname=template1 user=postgres password=%s" % password )
> c = db.cursor()
> c.execute( "drop database mytempdb;" )
> -- 
> - Tim Roberts, [EMAIL PROTECTED]
>   Providenza & Boekelheide, Inc.

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


Re: exception handling; python program that interacts with postgresql db

2006-08-21 Thread damacy
oh, fixed when i set isolation level to 0.
thanks anyway!

damacy wrote:
> thanks. i started to use psycopg.
>
> however, i have this error message and i don't quite get what it means.
>
> it says "DROP DATABASE cannot run inside a transaction block".
>
> does anyone have a clue?
>
> Tim Roberts wrote:
> > "damacy" <[EMAIL PROTECTED]> wrote:
> >
> > >hi, there. i have this question which might sound quite stupid to some
> > >people, but here we go anyway.
> > >
> > >i have written a python program which interacts with a postgresql
> > >database. what it does is simply drops an existing database called
> > >'mytempdb'.
> > >
> > >the code looks like below;
> > >
> > >link = subprocess.Popen(command, stdin = subprocess.PIPE, stdout =
> > >subprocess.PIPE, shell = True)
> > >link.communicate(password)
> > >link.wait()
> > >
> > >where command looks like "psql -h 127.0.0.1 -U postgres -W -f filename"
> > >and
> > >filename is the name of the file which contains a single SQL command
> > >which is "drop database mytempdb".
> >
> > hiaips is right.  The right way to do this is to use a Postgres module.
> > psycopg is my favorite, but there are several alternatives.
> >
> > import psycopg
> > db = psycopg.connect(
> > "dbname=template1 user=postgres password=%s" % password )
> > c = db.cursor()
> > c.execute( "drop database mytempdb;" )
> > --
> > - Tim Roberts, [EMAIL PROTECTED]
> >   Providenza & Boekelheide, Inc.

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


python + postgres psql + os.popen

2006-06-21 Thread damacy
hello, everyone.

i am trying to write a program which executes SQL commands stored in
.sql files.

i wrote a function called psql() whose contents look like the
following.

...
os.popen(command)
file = os.popen(command, 'w')
file.write(password)
file.close()
...

where command looks like
psql -h [host] -d [dbname] -U [username] -W -f "[filename]"

this works well. however, it does not show me any warning nor error
messages if there is one. for example, i am trying to create a table
which already exists in the database, it should show me a warning/error
message saying there already is one present in the database, or
something like that.

can anyone help me?

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


Re: python + postgres psql + os.popen

2006-06-23 Thread damacy
hi, there. thanks for the help.

now i have a different problem now. i decided to use 'subprocess' and
'Popen' objects instead of 'os.popen()' function, which i believe do
not make much difference.

my code is like the following...

[1] link = subprocess.Popen(command, stdin = subprocess.PIPE, stdout =
subprocess.PIPE, stderr = subprocess.PIPE, shell = True)
[2] link.communicate(password)
[3] link.wait()
[4] err = link.communicate()[1]
[5] if err != None: print str(err)

i have read several threads about 'subprocess' posted on this group and
still i have way too much confusion regarding the above section of
code.

1. i'm currently using MS Windows.
i remember some have said that communicate() function is not usable on
this OS.
could anyone confirm this?

2. i'm expecting an error message, as i am trying to create a table
which does already exist in the database.
but if i try to print out the error message as [5], it is just an EMPTY
string.
and, if i try the SAME THING using command-line, i get a correct error
message this time ('psql:createstudent.sql:12: ERROR:  relation
"student" already exists').

HOWEVER, if i comment out [2] link.communicate(password), meaning i do
not supply a password, it shows an error message, 'psql: fe_sendauth:
no password supplied', which is correct as expected.

my question is...
why does it work (i.e. showing a correct error message) when no
password supplied but NOT when creating a table which already exists in
the database? it should work for both cases.

thank you very much.


Simon Forman wrote:
> damacy wrote:
> > hello, everyone.
> ...
> > this works well. however, it does not show me any warning nor error
> > messages if there is one. for example, i am trying to create a table
> > which already exists in the database, it should show me a warning/error
> > message saying there already is one present in the database, or
> > something like that.
> >
> > can anyone help me?
>
> I recently needed to use psql from python on a computer that I couldn't
> install psycopg on and I used something similar to this to do it (I
> edited the code slightly to make it clearer):
>
> from subprocess import Popen, PIPE
>
> # Pass the password through an environment
> # variable to prevent psql asking for it.
> psql_env = dict(PGPASSWORD='')
>
> # Create the subprocess.
> proc = Popen(cmd, shell=True, env=psql_env, stdout=PIPE, stderr=PIPE)
>
> # Try reading it's data.
> data = proc.stdout.read()
>
> # Check for errors.
> err = proc.stderr.read()
> if err: raise Exception(err)
>
> 
> It worked nicely for me, YMMV.
> 
> 
> Hope that helps,
> 
> ~Simon

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


Re: python + postgres psql + os.popen

2006-06-23 Thread damacy
hi, there. thanks for the help.

now i have a different problem now. i decided to use 'subprocess' and
'Popen' objects instead of 'os.popen()' function, which i believe do
not make much difference.

my code is like the following...

[1] link = subprocess.Popen(command, stdin = subprocess.PIPE, stdout =
subprocess.PIPE, stderr = subprocess.PIPE, shell = True)
[2] link.communicate(password)
[3] link.wait()
[4] err = link.communicate()[1]
[5] if err != None: print str(err)

i have read several threads about 'subprocess' posted on this group and
still i have way too much confusion regarding the above section of
code.

1. i'm currently using MS Windows.
i remember some have said that communicate() function is not usable on
this OS.
could anyone confirm this?

2. i'm expecting an error message, as i am trying to create a table
which does already exist in the database.
but if i try to print out the error message as [5], it is just an EMPTY
string.
and, if i try the SAME THING using command-line, i get a correct error
message this time ('psql:createstudent.sql:12: ERROR:  relation
"student" already exists').

HOWEVER, if i comment out [2] link.communicate(password), meaning i do
not supply a password, it shows an error message, 'psql: fe_sendauth:
no password supplied', which is correct as expected.

my question is...
why does it work (i.e. showing a correct error message) when no
password supplied but NOT when creating a table which already exists in
the database? it should work for both cases.

thank you very much.


Simon Forman wrote:
> damacy wrote:
> > hello, everyone.
> ...
> > this works well. however, it does not show me any warning nor error
> > messages if there is one. for example, i am trying to create a table
> > which already exists in the database, it should show me a warning/error
> > message saying there already is one present in the database, or
> > something like that.
> >
> > can anyone help me?
>
> I recently needed to use psql from python on a computer that I couldn't
> install psycopg on and I used something similar to this to do it (I
> edited the code slightly to make it clearer):
>
> from subprocess import Popen, PIPE
>
> # Pass the password through an environment
> # variable to prevent psql asking for it.
> psql_env = dict(PGPASSWORD='')
>
> # Create the subprocess.
> proc = Popen(cmd, shell=True, env=psql_env, stdout=PIPE, stderr=PIPE)
>
> # Try reading it's data.
> data = proc.stdout.read()
>
> # Check for errors.
> err = proc.stderr.read()
> if err: raise Exception(err)
>
> 
> It worked nicely for me, YMMV.
> 
> 
> Hope that helps,
> 
> ~Simon

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


all ip addresses of machines in the local network

2006-08-23 Thread damacy
hi, there. i have a problem writing a program which can obtain ip
addresses of machines running in the same local network.

say, there are 4 machines present in the network; [a], [b], [c] and [d]
and if i run my program on [a], it should be able to find "host names"
and "ip addresses" of the other machines; [b], [c] and [d]?

i have read some threads posted on this group, however, they only work
for localhost, not the entire network.

any hints if possible?

thanks for your time.

regards, damacy

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


Re: all ip addresses of machines in the local network

2006-08-23 Thread damacy
hi, sandra.

no, it's not as complicated as that. all i want to do is to load a
database onto different machines residing in the same network. i hope
there is a way doing it. or perhaps i have a poor understanding of how
networks work.

regards, damacy

Sandra-24 wrote:
> damacy wrote:
> > hi, there. i have a problem writing a program which can obtain ip
> > addresses of machines running in the same local network.
> >
> > say, there are 4 machines present in the network; [a], [b], [c] and [d]
> > and if i run my program on [a], it should be able to find "host names"
> > and "ip addresses" of the other machines; [b], [c] and [d]?
> >
> > i have read some threads posted on this group, however, they only work
> > for localhost, not the entire network.
> >
> > any hints if possible?
> >
> > thanks for your time.
> >
> > regards, damacy
>
> What is this for? Some kind of high availablity server setup? I don't
> know anything that would be useful to you, but I am curious, and maybe
> it will clarify your intentions for others.
> 
> -Sandra

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


Re: all ip addresses of machines in the local network

2006-08-30 Thread damacy
Amit Khemka wrote:
> On 8/24/06, Amit Khemka <[EMAIL PROTECTED]> wrote:
> > On 23 Aug 2006 21:46:21 -0700, damacy <[EMAIL PROTECTED]> wrote:
> > > hi, sandra.
> > >
> > > no, it's not as complicated as that. all i want to do is to load a
> > > database onto different machines residing in the same network. i hope
> > > there is a way doing it. or perhaps i have a poor understanding of how
> > > networks work.
> > >
> >
> > I expect that you would know the IP range for your network. Then you
> > can simply 'ping' each IP in the range to find wether its alive.
> > Moreover by your description I guess you would actually want to find
> > all machines in your network that run a particular network service, to
> > allow you to "distribute the database". In such case you can use
> > "nmap" with -p option, to find all the machines which are listening on
> > the particular port.
> >
> > hth,
> > amit.
> It seems that I am not too busy, so here is a code which may work with
> a few tweaks here and there:
> _
> import os
> # base and range of the ip addresses
> baseIP = "10.0.0."
> r = 6
> interestingPort = 22 # port that you want to scan
> myIPs = []
>
> for i in range(r):
> ip = baseIP+str(i)  # It may need some customization for your case
> print "scanning: %s" %(ip)
> for output in os.popen("nmap %s -p %s" %(ip,
> interestingPort)).readlines():
> if output.__contains__('%s/tcp open'
> %interestingPort):  # i guess it would be tcp
> myIPs.append(ip)
> __
> print myIPs
>
>
> hth,
> amit.
> --
> 
> Amit Khemka -- onyomo.com
> Home Page: www.cse.iitd.ernet.in/~csd00377
> Endless the world's turn, endless the sun's Spinning, Endless the quest;
> I turn again, back to my own beginning, And here, find rest.

thank you for your code. i had a look at nmap and i think it's got some
cool features in it. however, i found it quite slow in my case as it
takes extra time to process the output.

in my program so far, multiple threads (255 threads in total) spawned
at once with each one of them trying to call socket.gethostbyaddr(ip)
function. i.e. if exception thrown, no machine found. i used .join() to
wait for the threads to terminate. it's fully working however the
problem is that it's too slow. it takes approx. 14 seconds to process
(i tried using 'ping' but it's even slower.).

my question is.. is there a way to improve performance of the program
if i know what the port number would be? in my case, the port number
will always be constant although i have no clue on what ip addresses
would be (that's the reason all of 255 different addresses must be
tested).

i tried the same function with the port number specified,
gethostbyaddr(ip:portno), but it is even 10-second slower than using
the same function without a port number specified (i.e. approx. 25
seconds to complete).

could anyone think of a better way of solving this problem?

regards, damacy

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


Re: all ip addresses of machines in the local network

2006-08-31 Thread damacy
Amit Khemka wrote:
> > in my program so far, multiple threads (255 threads in total) spawned
> > at once with each one of them trying to call socket.gethostbyaddr(ip)
> > function. i.e. if exception thrown, no machine found. i used .join() to
> > wait for the threads to terminate. it's fully working however the
> > problem is that it's too slow. it takes approx. 14 seconds to process
> > (i tried using 'ping' but it's even slower.).
> >
> > my question is.. is there a way to improve performance of the program
> > if i know what the port number would be? in my case, the port number
> > will always be constant although i have no clue on what ip addresses
> > would be (that's the reason all of 255 different addresses must be
> > tested).
> >
> > i tried the same function with the port number specified,
> > gethostbyaddr(ip:portno), but it is even 10-second slower than using
> > the same function without a port number specified (i.e. approx. 25
> > seconds to complete).
>
> You can save some (DNS) overheads by escaping the call
> "gethostbyaddr", assuming you are not interested in knowing the
> 'Names' of the machines in your Network. And directly attempt to find
> the machines which are listenting on the specified port. A simple way
> of
> doing this would be to use socket.connect((ip, port)), if the
> connections succeds you have
> your machine !
>
> ( There are various other ways of scanning ports, have a look at:
> http://insecure.org/nmap/nmap_doc.html#connect )
>
> Though I am not sure how 'fast' it would be. Also remember that the
> time in scanning is affected by network-type,
> response-time-of-remote-machine, number-of-machines scanned etc.
>
> I would still be interested, in seeing how nmap(with some smart
> options) compares with the python code. ( In my network "nmap
> -osscan_limit -p 22 -T5 Class_D_Network" completes in 1.5 seconds !)
>
> cheers,
> amit.
> --
> 
> Amit Khemka -- onyomo.com
> Home Page: www.cse.iitd.ernet.in/~csd00377
> Endless the world's turn, endless the sun's Spinning, Endless the quest;
> I turn again, back to my own beginning, And here, find rest.

hello.

here are my test results;

1. using nmap with the options specified above: approx. 50 seconds or
longer
2. using socket.connect((ip, port)): approx. 26 seconds
3. using socket.gethostbyaddr(ip): approx. 14 seconds

all three above use multiple threads.

and also, i tried using (in and out) queues to detect the threads
termination (instead of .join()), however, it's also very slow and it
needs to have deadlock detection mechanism implemented in it which
would probably lower the performance of the program.

hmm...

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