Re: [Tutor] File upload from python shell

2007-10-14 Thread Alan Gauld
"Paulino" <[EMAIL PROTECTED]> wrote

> How can I upload a file from python?

Just to be clear. You want to send a file from your computer
to another computer?

The simplest way to do that is using ftp. There is an ftp module.
However for that to work the receiving computer needs to be
running an ftp server.

> If it is a form to fill with values it's simple:
>
> urlopen("http://site.com/action?key1=value1;key2=value2";)
> and I get the form filled.

That may not always work if the form relies on an HTTP/POST
request rather than a GET request. I'm not sure how you get
urllib to use POST. Hmm, checking the docs it says:

---
  urlopen( url[, data])

Open the URL url, which can be either a string or a Request object.
data may be a string specifying additional data to send to the server,
or None if no such data is needed. Currently HTTP requests are
the only ones that use data; the HTTP request will be a POST
instead of a GET when the data parameter is provided.
data should be a buffer in the standard 
application/x-www-form-urlencoded
format. The urllib.urlencode() function takes a mapping or sequence
of 2-tuples and returns a string in this format.
--

> What about uploading a file programmaticaly?

If you mean you want to submit a web form that requires a file
then the form will use a POST request, so you will need to create
a data string for the urlopen call using urlencode.

However, how a file would be encoded into a string I don't know!
I''ve never used POST requests in urllib. From a scan of the module
it looks like you may need to use a FileOpener object but its
not clear to me how that works.

Hopefully someone else has done this already and can help.

Alan G


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] 2 problems in a small script

2007-10-14 Thread Dick Moores


At 06:48 PM 10/12/2007, Ian Witham wrote:

On 10/12/07, Dick Moores
<[EMAIL PROTECTED]> wrote:


Please see the code and it's output here:

<
http://www.rcblue.com/Python/buggy_For_Web.py>


I'm attempting to eliminate the elements (strings) of lstA that are


not well-formed in that they contain at least one character that
is

not in the string astr.


 
Hi Dick,
If your interested, this could be done succinctly with set types. (no
iterating required!) 
Hey, I'm VERY interested.
def well_formed(word):
    return not set('#.<@') & set(word)
>>> well_formed('porkpie')
True
>>> well_formed('p#rkpie')
False
And then it seems you are coming to terms with list comprehensions...

my_list = [word for word in my_list if
well_formed(my_word)]
Done. I had no idea set could be used that way! With a couple of
modifications, I used your ideas in both  and
 (see the thread I started,
<
http://www.nabble.com/An-idea-for-a-script-tf4603558.html#a13149808
>).
OR...
my_list = filter(well_formed, my_list)
I'll look into "filter".
A question:
Here's my(yours) well_formed():
def well_formed(word, astr):
    """
    Returns True if set() - set() is empty; False if not.
    """
    return not set(word) - set(astr)
Is it OK to use  like that in a docstring?
Thanks, Ian.
Dick


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] File upload from python shell

2007-10-14 Thread David Clymer
On Sun, 2007-10-14 at 01:42 +0100, Paulino wrote:
> Hello!
> 
> 
> How can I upload a file from python?
> 
> If it is a form to fill with values it's simple:
> 
> 
> urlopen("http://site.com/action?key1=value1;key2=value2";) and I get the 
> form filled.
> 
> 
> What about uploading a file programmaticaly?


Google tells me that urllib2 does not support file uploads properly,
however it also tells me that one can use httplib directly to do it:

http://wiki.forum.nokia.com/index.php/How_to_upload_a_file_to_server_with_application/x-www-form-urlencoded

-davidc

-- 
gpg-key: http://www.zettazebra.com/files/key.gpg


signature.asc
Description: This is a digitally signed message part
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Variables in workspace

2007-10-14 Thread Eli Brosh
Many thanks to Bob and Kent and all the good people in the tutor forum.
The dir() and del work really well !
 
Eli



מאת: bob gailer [mailto:[EMAIL PROTECTED]
נשלח: ש 10/13/2007 17:35
אל: Kent Johnson
עותק לידיעה: Eli Brosh; tutor@python.org
נושא: Re: [Tutor] Variables in workspace



Kent Johnson wrote:
> bob gailer wrote:
>> The del statement is the way to delete variables. Since dir() gives
>> you their names one needs use eval.
>>
>> for varName in dir():
>> eval 'del ' + varName
>
> I think del globals()[varName] would work.
Yep. That was nagging a corner of my brain, but not surfacing.




___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] upgrading Python

2007-10-14 Thread LandSurveyor
Thanks for the 'meaty' response.  Therein was the nitty-gritty I could actually 
use.  Learned a bunch too...had never used a link before.  Didn't know why I 
would want to.  BTW, I met with HUGE success!

Anyway...my approach was to take the road of a user (with my own '/home' 
account, and no SU privileges.  That only went so far.  I installed the new 
Python (2.5.1) in a directory within my home that I called, incisively, 
'/usrlowell'.  Therein I opened up the .tgz file, and got as far as 'make', 
when it required me to have SU privileges (so much for the theory that I could 
do all I wished within my own 'world').  That speed bump has undermined a 
number of concepts I thought I'd figured out-regarding autonomy for a user-so I 
don't know now what I don't know.).

Long and the short of it, the 'make' and 'make install' continued in the 
/usr/bin environ (i.e., departed from my '/home' directories).  I did not at 
first 'rm' the /usr/bin/python file-was too nervous to try it!  What I did, 
though was go into my new dedicated /home/usrlowell/'newPythonPlace' directory, 
and found another file named 'python*'.  On command line I entered ./python, 
and sure enough I got python 2.5.1.  Wow!  Then I went back to /usr/bin, erased 
the 'python' file, created a link to my dedicated (/home/etc...etc) file, and 
sure enough, "which python" continues to yield /usr/bin/python, but "python" 
brings up the new 2.5.1.

The experience has shaken some of my established ideas of how Linux works, but 
it's been a great leap forward for me (as someone once said, "Do that which you 
are afraid to do").

Thanks for all the help.   BTW, I think-with this post-I've figured out how to 
correctly reply within this tutor envronment.  I THINK!?

-Original Message-
>From: bhaaluu <[EMAIL PROTECTED]>
>Sent: Oct 13, 2007 5:47 PM
>To: LandSurveyor <[EMAIL PROTECTED]>
>Cc: tutor@python.org
>Subject: Re: [Tutor] upgrading Python
>
>Greetings,
>On my system I can have several python versions installed because
>they install as python2.3, python2.4, etc.
>
>$ which python
>/usr/bin/python
>$ ls -l /usr/bin/python
>... /usr/bin/python -> python2.4
>
>Here, we can see that "python" is just a link to /usr/bin/python2.4
>So, I can install python 2.5, and simply change the link so when I
>enter "python" at the prompt, it will start python2.5.
>
>$ man ln
>ln [OPTION]... [-T] TARGET LINK_NAME   (1st form)
>
>So, after installing python 2.5, change to /usr/bin, remove /usr/bin/python
>$ cd /usr/bin
>$ rm python
>
>And now make a new link to python2.5 (you may have to be root!):
># ln -s /usr/bin/python2.5 python
>
>If I'm not mistaken, the library files, and so forth are stored in different
>directories, so installing a new version, won't corrupt an older version.
>/lib/python2.4/site-packages
>
>See how python 2.4 is in the python2.4 directory?
>
>If, for some reason, you want the other version, just start it with the
>absolute pathname:
>$ /usr/bin/python2.3
>
>I hope this is helpful? I don't use Mandrake, so I'm unfamiliar with
>its package manager, or how to do upgrades for it. The above are
>generic *nix commands that should work with any distro.
>
>apt-get and Synaptic rock! =)
>-- 
>b h a a l u u at g m a i l dot c o m
>http://www.geocities.com/ek.bhaaluu/index.html
>
>On 10/13/07, LandSurveyor <[EMAIL PROTECTED]> wrote:
>> I wish to upgrade Python from the [Vers.] 2.3.4 that came packaged with my 
>> Mandrake 10.1 Linux OS to the current 2.5.1.  The 'hash/bang' line of my 
>> python scripts is "#!/usr/bin/python".  There are two files, both 
>> executables, in my /usr/bin directory; they are 1)python, and 2)python2.3.
>>
>> I just simply don't know what to do next!?  The advise I can google to is 
>> typically overly generous, full of contradictions (sometime within the same 
>> post..."Well you can do this, but if you wanna do that instead...").  Well, 
>> I don't know why "I want to do this", or "do that instead".  I just want to 
>> know where to put my new version of python, and when I unzip/configure/and 
>> so on..., will I end up with:
>>
>> 1)an application that will pick up seamlessly and run my apps?
>> 2)will pythontutor still be available?
>> 3)will I have an upgraded & accessible package of modules?
>> 4)will I need to modify the "hash/bang" line?
>> 5)when I type 'python' on a command line, how will I access the new 2.5.1 
>> rather than the older version?  i.e., where do I go to modify the results of 
>> that request?
>>
>> Oh, and could I-as I understand (to a limited degree) Linux- install the 
>> entire package within my home directory, following the principle that I am a 
>> user without admin privileges, and then change the "hash/bang" line to 
>> redirect to my 'embedded' version, and thus be running 2.5.1 within my own 
>> little world?  And if I did so, the same questions persist.  If I asked for 
>> a module, would I get the one from my 2.5.1, or the module that exists in 
>> the older 2.3.4?  And when

Re: [Tutor] File upload from python shell

2007-10-14 Thread Luke Paireepinart
Paulino wrote:
> Hello!
>
>
> How can I upload a file from python?
>
> If it is a form to fill with values it's simple:
>
>
> urlopen("http://site.com/action?key1=value1;key2=value2";) and I get the 
> form filled.
>   
Only if the form uses the GET method.
If your file upload form used GET, you could upload your file like this 
as well (after base-64 encoding it.)
I recommend reading the Wikipedia articles on GET and POST so you can 
know the difference.
>
> What about uploading a file programmaticaly?
>   
file uploads usually use the POST method, not GET.
they're generally the same thing as text or radiobutton form entries, 
except their input type is set to file,
so that it's base-64 encoded so that there's no errors in transmission.
basically you'll have to look at a few things:
1. how the POST method is structured.
2. Which keys your file-uploading page wants.
3. how to base-64 encode your file.
4. how to measure the length of all your keys and data so that you can 
set content-length header correctly.
5. how to submit this new http request properly.

What are you trying to upload to?
if it's ImageShack by any coincidence, I have some code that does that 
already.
If it's something else, the code would probably be similar.
Let me know if you want to take a look at that.
-Luke
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] 2 problems in a small script

2007-10-14 Thread Dick Moores
**Blush**

These lines were in my last post in this thread:

"Done. I had no idea set could be used that way! With a couple of 
modifications, I used your ideas in both  and 
 (see the thread I started, 
)."

They should have read:

"Done. I had no idea set could be used that way! With a couple of 
modifications, I used your ideas in both 
 and 
 (see the thread I 
started, 
)."

But also, though I'm very glad Ian showed me how to use sets in that 
magic way, I had forgotten that a list comprehension can be much more 
succinct:

lstB = [ word for word in lstA if all(char in astr for char in word) 
]  in "buggy" (line 11)

lstD = [ word for word in lstC if all(char in astr for char in word) 
] in "getCrew" (line 26)

(Thanks to Kent, I believe.)

These further revisions can be seen at 
 and 
.

Dick

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] upgrading Python

2007-10-14 Thread LandSurveyor
 Some interesting (!?) things happened when I upgraded from Python 2.3 to Python 2.5.1.  My editor of choice is Vim, the platform is MandrakeLinux 10.1...the digraphs I have incorporated from the Vim environment into my scripts no longer work.  The resultant error message referred me to pep-0263Within my code, I have two adjacent 'try/except' sequences.  The 'try' line waits for user input, and the 'except' offers default data if a response is bypassed.  What happens now is that both of those 'try' queries print simultaneously on the same line, i.e., not in sequence, the 2nd one following a response to the first.This is what I have discovered so far.   What could be happening?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] upgrading Python

2007-10-14 Thread bhaaluu
On 10/14/07, LandSurveyor <[EMAIL PROTECTED]> wrote:
> Long and the short of it, the 'make' and 'make install' continued in the 
> /usr/bin environ (i.e., departed from my '/home' directories).  I did not at 
> first 'rm' the /usr/bin/python file-was too nervous to try it!  What I did, 
> though was go into my new dedicated /home/usrlowell/'newPythonPlace' 
> directory, and found another file named 'python*'.  On command line I entered 
> ./python, and sure enough I got python 2.5.1.  Wow!  Then I went back to 
> /usr/bin, erased the 'python' file, created a link to my dedicated 
> (/home/etc...etc) file, and sure enough, "which python" continues to yield 
> /usr/bin/python, but "python" brings up the new 2.5.1.
>
> The experience has shaken some of my established ideas of how Linux works, 
> but it's been a great leap forward for me (as someone once said, "Do that 
> which you are afraid to do").
>
> Thanks for all the help.   BTW, I think-with this post-I've figured out how 
> to correctly reply within this tutor envronment.  I THINK!?

Linus Torvalds designed Linux with one objective: FUN.
The cool thing about GNU/Linux is: if something breaks,
you get to keep BOTH halves. =)  ALL the source code
is available, from the first assembly language bits, all
the way up to the meaty stuff, like X-Windows, and everything
in between, and on top of, and behind.
GNU/Linux is a programmer's heaven.
It is infinitely extensible, and configurable.

Why people even give MS any mind-share is beyond me
(Well, other than they maintain an illegal monopoly...
but we won't go there.)

Python is free and completely open.
Python and Linux were started about the same time (c.1991);
so they are like, the same age, more or less. =)

There are over 200 different Linux LiveCDs available, customized
for all sorts of different things. They are bootable. When they boot,
they create a RAM-disk, and run from that. So the hard drive is
not touched. Apps are dynamically uncompressed and loaded from
CD into the RAM-disk (a small performance hit here, when running
from LiveCD), but otherwise, the Linux LiveCDs are an excellent way
to test drive GNU/Linux, try it out, look at Free Software, and become
familiar with it. After all, familiarity is what makes MS-Windows seem so
easy. Once you become familiar with GNU/Linux, you find it is much
easier than MS-Windows! (I always feel like I'm chained to a huge steel
ball when I have to use a MS-Windows PC - that's how kludgey it its).

FrozenTech has a pretty good list of Linux LiveCDs.
Python is *usually* installed by default on a LiveCD.
Some popular Linux LiveCDs:
Knoppix
SimplyMEPIS
Kubuntu
-- 
b h a a l u u at g m a i l dot c o m
http://www.geocities.com/ek.bhaaluu/index.html
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] File upload from python shell

2007-10-14 Thread Paulino
Luke Paireepinart escreveu:
> Paulino wrote:
>> Hello!
>>
>>
>> How can I upload a file from python?
>>
>> If it is a form to fill with values it's simple:
>>
>>
>> urlopen("http://site.com/action?key1=value1;key2=value2";) and I get 
>> the form filled.
>>   
> Only if the form uses the GET method.
> If your file upload form used GET, you could upload your file like 
> this as well (after base-64 encoding it.)
> I recommend reading the Wikipedia articles on GET and POST so you can 
> know the difference.
>>
>> What about uploading a file programmaticaly?
>>   
> file uploads usually use the POST method, not GET.
> they're generally the same thing as text or radiobutton form entries, 
> except their input type is set to file,
> so that it's base-64 encoded so that there's no errors in transmission.
> basically you'll have to look at a few things:
> 1. how the POST method is structured.
> 2. Which keys your file-uploading page wants.
> 3. how to base-64 encode your file.
> 4. how to measure the length of all your keys and data so that you can 
> set content-length header correctly.
> 5. how to submit this new http request properly.
>
> What are you trying to upload to?
> if it's ImageShack by any coincidence, I have some code that does that 
> already.
> If it's something else, the code would probably be similar.
> Let me know if you want to take a look at that.
> -Luke
>
>
I want to upload pdf files, to an intranet server, that I also control.

Yes I think it would be very usefull to have a look at your code.

The urlopen function can take an optional data parameter that is used to 
specify a POST request, but i couldn't find information about how to set 
the data parameter properly.

Thank You
Paulino
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] File upload from python shell

2007-10-14 Thread Luke Paireepinart
Paulino wrote:
> Luke Paireepinart escreveu:
>> Paulino wrote:
>>> Hello!
>>>
>>>
>>> How can I upload a file from python?
>>>
>>> If it is a form to fill with values it's simple:
>>>
>>>
>>> urlopen("http://site.com/action?key1=value1;key2=value2";) and I get 
>>> the form filled.
>>>   
>> Only if the form uses the GET method.
>> If your file upload form used GET, you could upload your file like 
>> this as well (after base-64 encoding it.)
>> I recommend reading the Wikipedia articles on GET and POST so you can 
>> know the difference.
>>>
>>> What about uploading a file programmaticaly?
>>>   
>> file uploads usually use the POST method, not GET.
>> they're generally the same thing as text or radiobutton form entries, 
>> except their input type is set to file,
>> so that it's base-64 encoded so that there's no errors in transmission.
>> basically you'll have to look at a few things:
>> 1. how the POST method is structured.
>> 2. Which keys your file-uploading page wants.
>> 3. how to base-64 encode your file.
>> 4. how to measure the length of all your keys and data so that you 
>> can set content-length header correctly.
>> 5. how to submit this new http request properly.
>>
>> What are you trying to upload to?
>> if it's ImageShack by any coincidence, I have some code that does 
>> that already.
>> If it's something else, the code would probably be similar.
>> Let me know if you want to take a look at that.
>> -Luke
>>
>>
> I want to upload pdf files, to an intranet server, that I also control.
Well, give us more information on the server.
If you have control over it, why bother using HTTP?  Why not use FTP or SSH?
They'd have more flexibility and probably robustness as well.
Is the OS Windows or Linux or something else?
-Luke
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] largest and smallest numbers

2007-10-14 Thread Dick Moores
At 06:01 PM 9/24/2007, Terry Carroll wrote:
>On Mon, 24 Sep 2007, Christopher Spears wrote:
>
> > How can I find the largest float and complex numbers?
>
>That's an interesting question..
>
>I just tried this:
>
>x = 2.0
>while True:
> x = x*2
> print x
> if repr(x) == "1.#INF": break
>
>to just keep doubling X until Python began representing it as infinity.
>My output:
>
>4.0
>8.0
>16.0
>32.0
>64.0
>128.0
>  . . .
>137438953472.0
>274877906944.0
>549755813888.0
>1.09951162778e+012
>2.1990232e+012
>4.3980465111e+012
>  . . .
>2.24711641858e+307
>4.49423283716e+307
>8.98846567431e+307
>1.#INF
>
>So I'd say, the answer is somewhere between 8.98846567431e+307 and double
>that.

I pinched it down some more (after dinner):

 >>> 1.79769313486e+308 * (1.001 ** 1160)
1.7976931348623151e+308
 >>> 1.79769313486e+308 * (1.001 ** 1161)
1.#INF

Dick Moores
Win XP


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor