A better way to read Image from a server and start download / load
Hi Expert, I'm a newbie to python and still on the process of learning on the go ... I have a webserver which has list of images to load on a Device Under Test (DUT) ... *Requirement is:* if the image is already present on the server, proceed with loading the image onto the DUT. if the image is not present on the server , then proceed with the download of the image and then upgrade the DUT. I have written the following code but I'm quite not happy with the way I have written this, because I have a feeling that it could have been done better using some other method/s Please suggest the areas where i could have done better and the techniques to do so.. Appreciate your time in reading this email and for your valuable suggestions. import urllib2 url = 'http://localhost/test' filename = 'Image60.txt' # image to Verify def Image_Upgrade(): print 'proceeding with Image upgrade !!!' def Image_Download(): print 'Proceeding with Image Download !!!' resp = urllib2.urlopen(url) flag = False list_of_files = [] for contents in resp.readlines(): if 'Image' in contents: c=(((contents.split('href='))[-1]).split('>')[0]).strip('"') # The content output would have html tags. so removing the tags to pick only image name if c != filename: list_of_files.append(c) else: Image_Upgrade() flag = True if flag==False: Image_Download() Thanks, Vijay Swaminathan -- http://mail.python.org/mailman/listinfo/python-list
Need some assistance on Regular expression in python
Hi Experts, I'm a new bie to python and need some assistance in the usage of regular expression. I have a string something like this: New builds available Version: 20120418-abcdef-1 (based on SDK 0.0.0.1) from the above string I want to extract the following text using regular expression 20120418-abcdef-1 0.0.0.1 I can do this by split but I feel it is an ineffective way of doing this. I tried using regular expression but could not narrow down. for example, I used sdk_version = re.search(r"SDK(.*)", lines,) print sdk_version.group(1) but this gave the version 0.0.0.1) along with the paranthesis I did not know how to elimate ')' .. Need some help here.. Thanks -Vijay -- http://mail.python.org/mailman/listinfo/python-list
Sending email in python
Hi All, I read through the smtplib and email modules of python and have come up with a simple program to send a email as an attachment. This module sends out an email but not as an attachment but in the body of the email. Any problem with this code? any insight would be greatly appreciated. import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText testfile = 'test.txt' msg = MIMEMultipart() msg['Subject'] = 'Email with Attachment' msg['From'] = 'swavi...@gmail.com' msg['To'] = 'swavi...@gmail.com' with open(testfile,'rb') as f: # Open the files in binary mode. Let the MIMEImage class automatically # guess the specific image type. img = MIMEText(f.read()) f.close() msg.attach(img) try: s = smtplib.SMTP('localhost') s.sendmail(msg['From'],[msg['To']],msg.as_string()) print 'Email with attachment sent successfully' s.quit() except: print 'Email with attachment could not be sent !!' -- Vijay Swaminathan -- http://mail.python.org/mailman/listinfo/python-list
NewBie Doubt in Python Thread Programming
Hi All, I'm new bie to thread programming and I need some assistance in understanding few concepts ... I have a very simple program which runs a thread and prints a string. import threading class MyThread(threading.Thread): def __init__(self, parent = None): threading.Thread.__init__(self) def run(self): print 'Hello World' def main(): for i in range(10): MyThread_Object = MyThread() print 'Object id is : ' , id(MyThread_Object) print 'Staring thread --> ' , MyThread_Object.getName() MyThread_Object.start() count = threading.activeCount() print 'The active thread count is: ' , count if __name__ == '__main__': main() When I run this, I could see 10 thread being called. But when I print the active thread count it is only 2. Need some understanding on the following. 1. How the total active thread is 2? 2. how do I stop a thread? does it get automatically stopped after execution ? 3. Am I totally wrong in understanding the concepts. 4. what is the difference between active_count() and activeCount() since both seem to give the same result. 5. is there a way to find out if the thread is still active or dead? Please help me in understanding .. -- Vijay Swaminathan -- http://mail.python.org/mailman/listinfo/python-list
Re: NewBie Doubt in Python Thread Programming
Sorry. My intention was not to send out a private message. when I chose reply to all, I was confused if this would start as a new thread. so just did a reply.. coming back, I have developed a GUI based on pyQT4 which has a run button. when I click on run, it invokes a command prompt and runs a .bat file. Till the execution of .bat file is over, I want the run button on the GUI to be disabled. so i thought of invoking the command prompt and running the .bat file on a thread so that I could monitor the status of the thread (assuming that the thread would be active till command prompt is active - correct me if I'm wrong). for this, the code that I had written is; # to invoke a command prompt and execute the .bat file. class RunMonitor(threading.Thread): def __init__(self, parent=None): threading.Thread.__init__(self) def run(self): print 'Invoking Command Prompt..' subprocess.call(["start", "/DC:\\Script", "scripts_to_execute.bat"], shell=True) A timer function to call this thread and monitor this thread.. def runscript(self): self.timer = QTimer() self.timer.connect(self.timer, SIGNAL("timeout()"),self.sendData) self.timer.start(1000) def sendData(self): if self.run_timer: run_monitor_object = RunMonitor() print 'Starting the thread...' run_monitor_object.start() self.run_timer = False if run_monitor_object.isAlive(): print 'Thread Alive...' else: print 'Thread is Dead' Any flaw in the logic? any other better ways of achieving this? On Wed, May 11, 2011 at 2:16 PM, Chris Angelico wrote: > I'm responding to this on-list on the assumption that this wasn't > meant to be private; apologies if you didn't intend for this to be the > case! > > On Wed, May 11, 2011 at 6:38 PM, vijay swaminathan > wrote: > > so If i understand correctly, once the run method of the thread is > executed, > > the thread is no more alive. > > Once run() finishes executing, the thread dies. > > > Actually, I'm trying to invoke a command prompt to run some script and as > > long as the script runs on the command prompt, I would like to have the > > thread alive. But according to your statement, the thread would die off > > after invoking the command prompt. is there a way to keep the thread > active > > till I manually close the command prompt? > > That depends on how the "invoke command prompt" function works. > > > A snippet of the code written is: > > # Thread definition > > class RunMonitor(QThread): > > def __init__(self, parent=None): > > QThread.__init__(self) > > def run(self): > > print 'Invoking Command Prompt..' > > subprocess.call(["start", "/DC:\\Scripts", > > "scripts_to_execute.bat"], shell=True) > > > > def sendData(self): > > > > if self.run_timer: > > run_monitor_object = RunMonitor() > > print 'Starting the thread...' > > run_monitor_object.start() > > self.run_timer = False > > > > if run_monitor_object.isAlive(): > > print 'Thread Alive...' > > else: > > print 'Thread is Dead' > > > > subprocess.call() will return immediately, so this won't work. But if > you use os.system() instead, then it should do as you intend. > > > to check the status of the thread repeatedly I have the QTimer which > would > > call the self.sendData() for every minute. > > > > self.timer = QTimer() > > self.timer.connect(self.timer, SIGNAL("timeout()"),self.sendData) > > self.timer.start(1000) > > I'm not really sure what your overall goal is. Can you explain more of > your high-level intentions for this program? There may be a much > easier way to accomplish it. > > Chris Angelico > -- > http://mail.python.org/mailman/listinfo/python-list > -- Vijay Swaminathan -- http://mail.python.org/mailman/listinfo/python-list
Re: NewBie Doubt in Python Thread Programming
I tried using QThread as well.. But the problem is, on the run method when i invoke the command prompt, it sends out the finished signal... I want it to send out the finished signal only on closing the command prompt that is invoked earlier in my process. guess some logic to be implement inside run() which monitors the command prompt / reports the status once the command prompt is closed. I tried running loop inside the run() to keep the thread active but no help.. class RunMonitor(QThread): def __init__(self, parent = None): QThread.__init__(self) def run(self): print 'Invoking command prompt...' subprocess.call(["start", "/DC:\\PerfLocal_PAL", "scripts_to_execute.bat"], shell=True) while True: pass def runscript(self): print 'Complete_file_Path inside Run script is : ' , self.complete_file_path file_operation.Generate_Bat_File(self.complete_file_path) self.run_monitor_object = RunMonitor() self.run_monitor_object.start() self.connect(self.run_monitor_object, SIGNAL("finished()"),self.threadstatus) On Wed, May 11, 2011 at 9:25 PM, Wojtek Mamrak wrote: > 2011/5/11 Chris Angelico : > > On Thu, May 12, 2011 at 1:16 AM, Wojtek Mamrak > wrote: > >> Is there any special reason you don't want to use QThread? > >> > http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qthread.html#details > > > > Other than that QThread is part of QT and threading isn't, what are > > the advantages of QThread? Is it possible (safe) to manipulate QT > > objects - in this case, the button - from a thread other than the one > > that created them? (If not, that would be a good reason for using > > QThread, which will fire an event upon termination.) > > > > > QThread provides mechanism of signals and slots ("from" and "to" the > thread), which are used across all pyQt. Unfortunately it is not > possible to use any widget classes in the thread (direct quote from > the docs). On the other hand signals can fire methods from the main > thread (running the app'a main loop), so this is not a big deal. > The signals are: > - finished > - started > - terminated > It is possible to block the thread, make it sleep, check whether the > thread is running, and few others. > -- > http://mail.python.org/mailman/listinfo/python-list > -- Vijay Swaminathan -- http://mail.python.org/mailman/listinfo/python-list
Need Assistance on this program.
Hi All, I'm new bie to python thread programming and would like to assistance on the attached code. In this, I'm calling a thread to invoke a command prompt and would like to print the "Thread as alive" as long as the command prompt is opened and would like to print "Thread is Dead" only when the command prompt is closed by the user. based on my understand I have written this peace of code which does not seem to work. There is some flaw in the logic which I could not understand since I'm not that familiar with the thread concepts. Can somebody help me in getting this piece of code working. Also can somebody point me to a good tutorial on understanding python thread programming since I want to get my understanding on the concept of thread better. I googled a few but quite confusing. -- Vijay Swaminathan Thread_Example1.py Description: Binary data -- http://mail.python.org/mailman/listinfo/python-list
Re: NewBie Doubt in Python Thread Programming
Hi Chris, I tried using os.system as well but it did not even open up the command prompt. Can you please share the code that worked for you.. just wondering if I'm missing something very basic. Regards, -Vijay Swaminathan., On Thu, May 12, 2011 at 1:38 PM, Chris Angelico wrote: > On Thu, May 12, 2011 at 3:35 PM, vijay swaminathan > wrote: > > I tried using QThread as well.. But the problem is, on the run method > when i > > invoke the command prompt, it sends out the finished signal... I want it > to > > send out the finished signal only on closing the command prompt that is > > invoked earlier in my process. > > > subprocess.call(["start", "/DC:\\PerfLocal_PAL", > > "scripts_to_execute.bat"], shell=True) > > This is your problem, still. You need to change to a call that waits. > In my testing on Windows (Python 2.6.5), this can be done with > os.system() quite happily. Change that, and it should all work. > > Chris Angelico > -- > http://mail.python.org/mailman/listinfo/python-list > -- Vijay Swaminathan -- http://mail.python.org/mailman/listinfo/python-list
Re: Need Assistance on this program.
I tried using that as well. The problem is, the thread becomes dead as soon as it executes the invocation of command prompt. I want the thread to be alive till I go and manually close the command prompt. -Vijay Swaminathan. On Thu, May 12, 2011 at 2:46 PM, Andrea Crotti wrote: > vijay swaminathan writes: > > > Hi All, > > > > I'm new bie to python thread programming and would like to assistance > > on the attached code. > > > > In this, I'm calling a thread to invoke a command prompt and would > > like to print the "Thread as alive" as long as the command prompt is > > opened and would like to print "Thread is Dead" only when the command > > prompt is closed by the user. > > > > based on my understand I have written this peace of code which does > > not seem to work. There is some flaw in the logic which I could not > > understand since I'm not that familiar with the thread concepts. > > > > Can somebody help me in getting this piece of code working. > > > > Also can somebody point me to a good tutorial on understanding python > > thread programming since I want to get my understanding on the concept > > of thread better. I googled a few but quite confusing. > > > > -- > > Vijay Swaminathan > > Easy mistake, this: > if mythread_object.is_alive: > > is wrong, since is_alive is a method, not a bool so it should be > is_alive(). > > Pay attention to these things, and actually something called > "is_adjective" is almost never a variable. > -- Vijay Swaminathan -- http://mail.python.org/mailman/listinfo/python-list
Re: Need Assistance on this program.
Hi Tim, I have already done that. But for some reason my response went as a new thread. Attaching the code again. On Thu, May 12, 2011 at 3:38 PM, Tim Golden wrote: > On 12/05/2011 10:45, vijay swaminathan wrote: > >> >> I tried using that as well. >> The problem is, the thread becomes dead as soon as it executes the >> invocation of command prompt. >> > > Can you post the code you're using, please? > > This should be simple so maybe you've misunderstood > something in the threading / subprocess mix. > > TJG > -- > http://mail.python.org/mailman/listinfo/python-list > -- Vijay Swaminathan Thread_Example1.py Description: Binary data -- http://mail.python.org/mailman/listinfo/python-list
Re: Need Assistance on this program.
Hi Tim., Thanks.. This works as I had expected. are there any documentation for the subprocess.call method? I tried going through the python doc but could not narrow down. I just wanted to know how do I pass an arguement after invoking the command prompt? Any thoughts on this pls? On Thu, May 12, 2011 at 4:08 PM, Tim Golden wrote: > On 12/05/2011 11:29, vijay swaminathan wrote: > > <... snippet from code ...> > print 'Invoking Command Promptt..' >#subprocess.call(["start", "/DC:\\PerfLocal_PAL", > "scripts_to_execute.bat"], shell=True) >subprocess.call(["start", "C:\\windows\\system32\\cmd.exe"], shell = > True) >self.status() > > > If you want to use start, use start /wait. > > But you don't have to: > > > import threading > import time > import subprocess > > def run_command (command): > # > # .call is shorthand for: start process and wait > # CREATE_NEW_CONSOLE prevents it from getting messed > # up with the Python console > # > subprocess.call ( >[command], >creationflags=subprocess.CREATE_NEW_CONSOLE > ) > > t = threading.Thread (target=run_command, args=("cmd.exe",)) > t.start () > > while t.is_alive (): > print "alive" > time.sleep (0.5) > > print "Thread is dead" > > > > > TJG > -- > http://mail.python.org/mailman/listinfo/python-list > -- Vijay Swaminathan -- http://mail.python.org/mailman/listinfo/python-list
Assistance in understanding the sub-Process module
Hi Gurus, I'm new to Python programming and in the process of learning the sub process module. I went through the python documentation http://docs.python.org/library/subprocess.html and I have the following queries 1. The class definition as per the documentation is: *class *subprocess.Popen(*args*, *bufsize=0*, *executable=None*, *stdin=None *, *stdout=None*, *stderr=None*, *preexec_fn=None*, *close_fds=False*, * shell=False*, *cwd=None*, *env=None*, *universal_newlines=False*, * startupinfo=None*, *creationflags=0*) *args* should be a string, or a sequence of program arguments. so I assume that args can be a string or a list with first item of the list being the program to execute. so on the python IDLE, I executed this command, >>> subprocess.Popen('cmd.exe') which opened up a command prompt. when I give this as a list, as below it throwed this error. >>> subprocess.Popen(['cmd.exe', 'dir']) Traceback (most recent call last): File "", line 1, in subprocess.Popen(['cmd.exe' 'dir']) File "C:\Python26\lib\subprocess.py", line 623, in __init__ errread, errwrite) File "C:\Python26\lib\subprocess.py", line 833, in _execute_child startupinfo) WindowsError: [Error 2] The system cannot find the file specified >>> I would assume that a list is accepted as part of the args and first being the program (cmd.exe) and the rest being the arguments... please correct me If i misunderstood. Thanks, -- Vijay Swaminathan -- http://mail.python.org/mailman/listinfo/python-list
Problem in using subprocess module and communicate()
Hi Gurus, I'm having some problem in using the communicate() along with the subprocess.I would like to invoke a command prompt and pass on a .bat file to execute. I went through the subprocess module and understood that using communicate, we can send the send data to stdin. According to the documentation http://docs.python.org/library/subprocess.html#subprocess.call, it says, if you want to send data to the process’s stdin, you need to create the Popen object with stdin=PIPE. so based on this, I have used the below function but it does not seem to work. Am I missing something here? import subprocess import threading import time def runMonitor(command): retcode = subprocess.Popen([command, '\k', 'dir'], cwd= 'C:\Python26\WorkSpace\FunctionExamples\src', stdin = subprocess.PIPE, creationflags=subprocess.CREATE_NEW_CONSOLE) retcode.wait() retcode.communicate('scripts_to_execute.bat') t = threading.Thread(target = runMonitor, args = ("cmd.exe",)) t.start() while t.is_alive(): print 'Thread is still alive' time.sleep(0.5) else: print 'Thread is dead' Vijay Swaminathan -- http://mail.python.org/mailman/listinfo/python-list