Re: pexpect with apache
Well, first i don't think it is a good idea to have the python script
tu su to root, but for it to work, i think (Totally unsure about that)
www has to be in group wheel to be able to su.
An other way to make your script run as root is to set the setuid bit
on your python script to make it run as root, without using su.
[EMAIL PROTECTED] wrote:
> Hi all. I try not to post until I am stuck in hole with no way out. I
> fought with this for several hours, and am currently in the hole.
>
> I'm doing a proof of concept for creating afp shares dynamically
> through a web interface from a client machine. I use a bit of php to
> setup a simple form, and then have the php execute my python script on
> the server. The python script tries to 'su' to root to create the
> share, create dirs, set perms, etc
>
> The python script alone works fine as 'www'. I can become 'www', run
> it from the command line and the share is made. But when I try to have
> the web server execute it, I continually get a password failure. I'm
> positive the password is correct.
>
> Any ideas?
>
> ~Sean D
>
> ~~~test.py
> #! /usr/bin/env python
>
> import commands, os, P, pexpect
>
> sharename = sys.argv[1]
>
> root = "/Users/Shared"
> sharepath = os.path.join(root, sharename)
> password = P.P()
>
> COMMAND_PROMPT = '[$%#]'
> child = pexpect.spawn('su')
> i = child.expect([pexpect.TIMEOUT, '[Pp]assword:'], timeout=1)
> child.sendline(password.Decrypt(password.sean))
>
> i = child.expect (['su: Sorry', COMMAND_PROMPT])
>
> if i == 0:
> print 'Password not accepted'
> sys.exit(1)
> else:
> print "Making dir: %s" % sharepath
> child.sendline("mkdir %s" % sharepath)
> i = child.expect([pexpect.TIMEOUT, COMMAND_PROMPT] ,timeout=1)
> print "Setting group to 'audio'"
> child.sendline("chgrp audio %s" % sharepath)
> i = child.expect([pexpect.TIMEOUT, COMMAND_PROMPT] ,timeout=1)
> print "setting owner to 'audio01'"
> child.sendline("chown audio01 %s" % sharepath)
> i = child.expect([pexpect.TIMEOUT, COMMAND_PROMPT] ,timeout=1)
> print "Opening permissions"
> child.sendline("chmod 777 %s" % sharepath)
> i = child.expect([pexpect.TIMEOUT, COMMAND_PROMPT] ,timeout=1)
> print "sharing -a %s -s 100" % sharepath
> child.sendline("sharing -a %s -s 100" % sharepath)
> i = child.expect([pexpect.TIMEOUT, COMMAND_PROMPT] ,timeout=1)
> sys.exit(0)
>
> ~~~test.php
>
>
>
> if (isset($_GET['sharename'])) {
> $last_line = system("/Users/Shared/test.py {$_GET['sharename']}",
> $retval);
> if ($retval == 0) {
> echo "Mount
> afp://xxx.xxx.xxx.xxx/{$_GET['sharename']}";
> } else {
> echo "Failed creating share!";
> }
> } else {
>
> echo "";
> echo "";
> echo "Name of share: name='sharename'>";
> echo "";
> }
>
> ?>
>
>
--
http://mail.python.org/mailman/listinfo/python-list
Re: pexpect with apache
Sudo is probably the best solution here, since in the file sudo.conf you could restrict the www user only to the python script that requires it. Also, using either sudo or the setuid flag would remove the need of pexpect since all the commands will be run as the designated user. for setuid flag: chmod u+s pythonScript.py chown root pythonScript.py for the sudo solution, add an entry to /etc/sudo.conf or /etc/sudoers , depending on distro: the syntax for a line in sudo.conf is: user hostlist = (userlist) commandlist so you might want to add: www localhost = NOPASSWD: /var/www/htdocs/pythonScript.py note: Replace the /var/www/htdocs/pythonScript.py with the path to where your script is the NOPASSWD: is a flag that tells sudo that no password is required Lee Harr wrote: > > Well, first i don't think it is a good idea to have the python script > > tu su to root, but for it to work, i think (Totally unsure about that) > > www has to be in group wheel to be able to su. > > > Maybe sudo can help here. -- http://mail.python.org/mailman/listinfo/python-list
Re: pexpect with apache
Since it wont require pyexpect, and based on the operations you accomplish with your python script, maybe that a bash script instead of a python one might be the best tool for the job you're trying to accomplish. martdi wrote: > Sudo is probably the best solution here, since in the file sudo.conf > you could restrict the www user only to the python script that requires > it. > > Also, using either sudo or the setuid flag would remove the need of > pexpect since all the commands will be run as the designated user. > > for setuid flag: > chmod u+s pythonScript.py > chown root pythonScript.py > > for the sudo solution, add an entry to /etc/sudo.conf or /etc/sudoers , > depending on distro: > the syntax for a line in sudo.conf is: > user hostlist = (userlist) commandlist > > so you might want to add: > www localhost = NOPASSWD: /var/www/htdocs/pythonScript.py > > note: > Replace the /var/www/htdocs/pythonScript.py with the path to where > your script is > the NOPASSWD: is a flag that tells sudo that no password is > required > > Lee Harr wrote: > > > Well, first i don't think it is a good idea to have the python script > > > tu su to root, but for it to work, i think (Totally unsure about that) > > > www has to be in group wheel to be able to su. > > > > > > Maybe sudo can help here. -- http://mail.python.org/mailman/listinfo/python-list
Re: Converting existing module/objects to threads
[EMAIL PROTECTED] wrote: > I have inheirted some existing code, that i will explain in a moment, > have needed to extend and ultimately should be able to run in threads. > I've done a bunch of work with python but very little with threads and > am looking for some pointers on how to implement, and if the lower > level modules/objects need to be rewritten to use threading.local for > all local variables. > > I have a module that communicates with a hardware device, which reads > data off of sensors, that can only talk with one controller at a time. > The controller (my module) needs to (in its simplest form) init, > configure the device, request data, and write out xml, sleep, repeat. > > The new request is that the device needs to be queried until a > condition is true, and then start requesting data. So an instance of a > controller needs to be deadicated to a hardware device forever, or > until the program endswhich ever comes first. > > This currently works in a non-threaded version, but only for one device > at a time, there is a need to create a single windows(yeach) service > that talks to many of these devices at once. I don't need worker > threads that handle seperate portions of the entire job, i need a > single application to spawn multiple processes to run through the > entire communication from configure to report, sleep until the next > interval time and run again. The communication could last from 1 > minute to 10 minutes before it ends. > > > Here is the code layout in pseudocode. > > module.Object - controller.Main - handles all socket communications > > class subcontroller(controller.Main): > def __init__(self,id,configurationFile): >controller.Main.__init__(self) >// instantiate variables and local objects that handle > configuration, logic and data output > > def configure(self,configurationFile): > //read configurationFile and configure device > > def process(self): > while 1: > //based on configuration file, query the device until condition > is true and then write xml, sleep until time to repeat and run again. > > within controller there are 5 objects and subcontroller is a sinlge > object that loads other objects from the inherited controller.System > > I'm trying to figure out how difficult it is going to be to convert > this to a threaded application. The original controller.Main is built > to talk to devices in series, never in parallel. so no objects are > considered to be thread safe, but no instance of any of the objects > should need to share resources with any other instance of teh same > object. they would all have unique configuration files and talk to > devices on unique ip/ports. > > on a unix system, forking,while potentially not optimal, would be a > fine solution, unfortunantely this needs to run on windows. > > I know i have left out many details, but hopefully this is enough to at > least enable some kind soles to lend an opinnion or two. > > many thanks > jd Taking a look at asyncore could be worthwhile, but if you want to implement it with threads, you may be able to do it this way: In your main file, from where you start the program, let's call it main: main(self) Load Required configuration spawn threads (1 for each controller) define a queue object from module queue.queue used for communication with threads enter an infinite loop that checks for the conditions once conditions are met, notify the proper thread class ControllerThread(threading.Thread): def __init__(self): define a queue here, to process messages from the main call threading.Thread.__init__(self) define method to load config for thread objects (you might want to pass an argument to init to load your configs from a file) (you might also want to pass the queue of the main program to the thread to send it messages) define methods to post messages to the queue like read, send to the machine, stop, ... define the run method that is what will be called when you start your thread. this method should enter an infinite loop that will check if something has to be done (check in the queue). hope this might help you good luck -- http://mail.python.org/mailman/listinfo/python-list
Re: Converting existing module/objects to threads
I am not sure if I understand you question well, but: in the __init__ of the thread subclass, you can instantiate an object of the class that makes the work or ControllerThread could extend both classes and i don't think there would be a problem. Problems in multithreading usually happen when many threads try to access the same ressource at the same time or that one thread waits for an other thread to finish, and that other thread waits for the first one to finish. The Queue module is threadsafe and it uses locking to prevent multiple threads to access it at the same time. Using Queue is a lot easier than having to manage locks by yourself. As long as you do not share data between your classes like static members, database connections, or I/0 on the same ressource, you probably won't encounter problems. [EMAIL PROTECTED] wrote: > martdi wrote: > > [EMAIL PROTECTED] wrote: > > > I have inheirted some existing code, that i will explain in a moment, > > > have needed to extend and ultimately should be able to run in threads. > > > I've done a bunch of work with python but very little with threads and > > > am looking for some pointers on how to implement, and if the lower > > > level modules/objects need to be rewritten to use threading.local for > > > all local variables. > > > > > > I have a module that communicates with a hardware device, which reads > > > data off of sensors, that can only talk with one controller at a time. > > > The controller (my module) needs to (in its simplest form) init, > > > configure the device, request data, and write out xml, sleep, repeat. > > > > > > The new request is that the device needs to be queried until a > > > condition is true, and then start requesting data. So an instance of a > > > controller needs to be deadicated to a hardware device forever, or > > > until the program endswhich ever comes first. > > > > > > This currently works in a non-threaded version, but only for one device > > > at a time, there is a need to create a single windows(yeach) service > > > that talks to many of these devices at once. I don't need worker > > > threads that handle seperate portions of the entire job, i need a > > > single application to spawn multiple processes to run through the > > > entire communication from configure to report, sleep until the next > > > interval time and run again. The communication could last from 1 > > > minute to 10 minutes before it ends. > > > > > > > > > Here is the code layout in pseudocode. > > > > > > module.Object - controller.Main - handles all socket communications > > > > > > class subcontroller(controller.Main): > > > def __init__(self,id,configurationFile): > > >controller.Main.__init__(self) > > >// instantiate variables and local objects that handle > > > configuration, logic and data output > > > > > > def configure(self,configurationFile): > > > //read configurationFile and configure device > > > > > > def process(self): > > > while 1: > > > //based on configuration file, query the device until condition > > > is true and then write xml, sleep until time to repeat and run again. > > > > > > within controller there are 5 objects and subcontroller is a sinlge > > > object that loads other objects from the inherited controller.System > > > > > > I'm trying to figure out how difficult it is going to be to convert > > > this to a threaded application. The original controller.Main is built > > > to talk to devices in series, never in parallel. so no objects are > > > considered to be thread safe, but no instance of any of the objects > > > should need to share resources with any other instance of teh same > > > object. they would all have unique configuration files and talk to > > > devices on unique ip/ports. > > > > > > on a unix system, forking,while potentially not optimal, would be a > > > fine solution, unfortunantely this needs to run on windows. > > > > > > I know i have left out many details, but hopefully this is enough to at > > > least enable some kind soles to lend an opinnion or two. > > > > > > many thanks > > > jd > > > > Taking a look at asyncore could be worthwhile, but if you want to > > implement it with threads, you may be able to do it this way: > > > > In your main file, from where you start the program, let's call it > >
Re: How to convert " " in a string to blank space?
一首诗 wrote:
> Is there any simple way to solve this problem?
>>> myString = " "
>>> myString = myString.replace(" ", "")
--
http://mail.python.org/mailman/listinfo/python-list
Re: Having trouble with file modes
> At first I was convinced that "w+" was the tool for the job. But now > I'm finding that the contents of the file are deleted (so I can't read > the data in). Possible File Modes: a : Append -- Add data at the end of the file r : Read -- Read from the file w : write -- Flush contents of the file and put data in it r+ : read and write -- Make changes to the file any of the above modes with b added at the end of the mode sets the file in binary mode you might want to check section 7.2 from http://docs.python.org/tut/node9.html Ofen when doing file operations you might prefer to read the file to modify, make changes in memory then save back in an other file, so you can make sure the changes are ok before you replace the old one -- http://mail.python.org/mailman/listinfo/python-list
Re: tips requested for a log-processing script
if you are running in windows you can use the win32com module to automate the process of generating a pivot table in excel and then code to send it via e-mail Jaap wrote: > Python ers, > As a relatively new user of Python I would like to ask your advice on > the following script I want to create. > > I have a logfile which contains records. All records have the same > layout, and are stored in a CSV-format. Each record is (non-uniquely) > identified by a date and a itemID. Each itemID can occur 0 or more times > per month. The item contains a figure/amount which I need to sum per > month and per itemID. I have already managed to separate the individual > parts of each logfile-record by using the csv-module from Python 2.5. > very simple indeed. > > Apart from this I have a configuration file, which contains the list of > itemID's i need to focus on per month. Not all itemID's are relevant for > each month, but for example only every second or third month. All > records in the logfile with other itemID's can be ignored. I have yet to > define the format of this configuration file, but am thinking about a 0 > or 1 for each month, and then the itemID, like: > "1 0 0 1 0 0 1 0 0 1 0 0 123456" for a itemID 123456 which only needs > consideration at first month of each quarter. > > My question to this forum is: which data structure would you propose? > The logfile is not very big (about 200k max, average 200k) so I assume I > can store in internal memory/list? > > How would you propose I tackle the filtering of relevant/non-relevant > items from logfile? Would you propose I use a filter(func, list) for > this task or is another thing better? > > In the end I want to mail the outcome of my process, but this seems > straitforward from the documentation I have found, although I must > connect to an external SMTP-server. > > Any tips, views, advice is highly appreciated! > > > Jaap > > PS: when I load the logfile in a spreadsheet I can create a pivot table > which does about the same ;-] but that is not what I want; the > processing must be automated in the end with a periodic script which > e-mails the summary of the keyfigure every month. -- http://mail.python.org/mailman/listinfo/python-list
Re: adding python scripting to my application
Jerry wrote: > I am not a Python guru by any means, but I believe that when an > application says that you can "script" their application with Python, > it means that you can actually write Python code to interact with the > application. Embedding may be the same thing. Extending (as I read > it) involves writing portions of your program in Python and have do the > logic portions. > > If you would like to allow users to write small scripts that interact > with your program and control it (a la VBScript or AppleScript), then I > believe embedding Python in your program is what you want. > > If you would like Python to run various parts of your program in order > to make some portions easier to write/manage/whatever, then you want to > "extend" your program using Python. > > The key difference being that you aren't providing a way for someone > else to interact with your program using Python if you are extending. > > Again, I am not a guru and a read through the docs on python.org would > probably be the best place to get sound technical advice on these > subjects. > > -- > Jerry I'm not sure either, but I though Extending was making the C/C++ Code available from Python, while Embedding was making Python Code Available in your app. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python v PHP: fair comparison?
> I'd be surprised if there was more demand for PHP developers > than Python developers. Google lists 51 PHP jobs and 168 > Python jobs in their internal jobs database (I just did a > quick search). Yes, but Google is the company that hired Guido, and that does most of it's dev in python. Looking at other companies yields different results: Workopolis: PHP 120 Python 36 Monster.ca: PHP more than 1000 Python 976 -- http://mail.python.org/mailman/listinfo/python-list
