Question about how I'm passing a stack from js to python.

For all coding issues - MODers and programmers, HTML and more.

Moderators: Jeff250, fliptw

Post Reply
User avatar
Isaac
DBB Artist
DBB Artist
Posts: 7127
Joined: Mon Aug 01, 2005 8:47 am
Location: Ơ̸̦͇̲̬̭̱̰͎̞͈̣͎͚̳ͬ͋̃̀̇͊͂͋͐ͦ̽ͣ̂ͥ͊̅̀̚͠ B̶͖̯͉̜̰̲̓̔͋̈́ͅ È̯ Y̪̤̼͉̠̙͝

Question about how I'm passing a stack from js to python.

Post by Isaac » Sun Apr 03, 2011 6:27 pm

Server side (python):

edit: can't use code box since formatting won't hold. Using screenshot instead:
Screenshot.jpg
Screenshot.jpg (18.03 KiB) Viewed 1074 times

Browser (javascript):

Code: Select all

<form action="/" method="post"><input type="hidden" name="ad" value="a3jk3j5k3j335h"><input type="hidden" name="ad" value="1"><input type="hidden" name="ad" value="1"><input type="hidden" name="ad" value="3"><input type="submit" value="+add user"></forum>
This button's data, once received by Python:

Code: Select all

>>>self.js
('a3jk3j5k3j335h','1','1','3')

You'll notice that I'm doing something weird by giving all the inputs the same name. On all big name browsers this is treated like a stack and python builds a friendly tuple from it. However, on some obscure browsers, like my nokia, whatever ipad uses, and my grandma's Netscape browser like to only send the first variable and ignores the rest...

Is there an easy way to simply reformat this <form> data to pass a stack with out scaring mobile and archaic browsers?



Thank you, internet people!
s☼-£♦и̫͍ͥ̍ͪ͌̓͗͡о̡̹̱͊̅ͮ̓̕͢б̧̝̻̪̤̳̜͐̓̉ͤ͢͜ ͙̬͙̆̑ͮ̐ͭ̾̂́͘i̎̌̾̓̽̀̈̓̀҉͉̙̦͎̘̝͕f̻͕͔̘ͣͣ̓͊̿͢͜ ͍͔͈͕̮̫ͣ̆ͮ̊͋/♂6Æ!♪╩"▲L└уͭ̂͐̇҉̴̣̼̞̠̯͓̺̞ф̜̊͌̈́̋̏̐́ц̨͔̮̿̇ ̨̛͖̙͖̖̮̗̱ͩ̆͞ͅа̥͇̞̖͚̟̅͐ͤ͞͠͠э̜̘̩̳̬͔̾ͯ̀ͫ̒̐̿ͅͅг̭̖̀ͦ̒̑ͥ̌ͮͫ͞ё͔̟̃ͬ̾̓͟ё̦̞̙̫͔̩͑̀͂ͯ̄̔̃̑̀͠ͅͅ
User avatar
Jeff250
DBB Master
DBB Master
Posts: 6387
Joined: Sun Sep 05, 1999 2:01 am
Location: ☃☃☃

Re: Question about how I'm passing a stack from js to python

Post by Jeff250 » Sun Apr 03, 2011 7:03 pm

HTTP allows multiple GET and POST parameters with the same name, so you're not doing anything that weird. From what you're saying, it looks like python+cgi presents this as a tuple, which is not uncommon (most frameworks use some sort of list/tuple/array abstraction for this). Do you have an exact example of a browser version that you think fails at this that one can install on a desktop and test? If it's an ancient version of Netscape, then it might just be broken, but I find your claim of Safari on the iPad failing at this a bit more alarming.

The problem might be something less complex, like if you're using XHTML, make sure to close your input tags.

It's not clear to me what Javascript has to do with any of this.

You can always just use name="foo1", name="foo2", etc., and build the tuple yourself, but that's not a very satisfying solution curiosity-wise.
User avatar
Jeff250
DBB Master
DBB Master
Posts: 6387
Joined: Sun Sep 05, 1999 2:01 am
Location: ☃☃☃

Re: Question about how I'm passing a stack from js to python

Post by Jeff250 » Sun Apr 03, 2011 7:07 pm

Also, some general code review:
* Is there a reason you're using """foo""" quotes instead of 'foo' quotes?
* If self.js[0] can contain arbitrary user input, then your code could be unsafe XSS-wise.
User avatar
Isaac
DBB Artist
DBB Artist
Posts: 7127
Joined: Mon Aug 01, 2005 8:47 am
Location: Ơ̸̦͇̲̬̭̱̰͎̞͈̣͎͚̳ͬ͋̃̀̇͊͂͋͐ͦ̽ͣ̂ͥ͊̅̀̚͠ B̶͖̯͉̜̰̲̓̔͋̈́ͅ È̯ Y̪̤̼͉̠̙͝

Re: Question about how I'm passing a stack from js to python

Post by Isaac » Sun Apr 03, 2011 8:35 pm

Jeff250 wrote:HTTP allows multiple GET and POST parameters with the same name, so you're not doing anything that weird. From what you're saying, it looks like python+cgi presents this as a tuple, which is not uncommon (most frameworks use some sort of list/tuple/array abstraction for this).
That's good! I was so sure this thing I had stumbled while experimenting was the wrong was of passing stacks.

Jeff250 wrote:Do you have an exact example of a browser version that you think fails at this that one can install on a desktop and test? If it's an ancient version of Netscape, then it might just be broken, but I find your claim of Safari on the iPad failing at this a bit more alarming.
And the android browser. I'll have to get more information, though I think I've got all that on Google Analytics. I don't get much traffic so it's easy for me sort out who's who.
Jeff250 wrote:The problem might be something less complex, like if you're using XHTML, make sure to close your input tags.
It's actually a single CGI file that imports a main module, which imports a sub module, then imports another submodule, and so on. So it's just one python script that grows depending on what string it's fed. It's probably a bad way of doing things.

Jeff250 wrote:It's not clear to me what Javascript has to do with any of this.
Oh sorry. I learned about <form> from a javascript site and since it returned data I figured it was javascript related... I should have known it wasn't since I never typed "javacript" into my site. Sorry sorry!

Also, I've decided to start working with javascript a bit more (And by that I mean real javascript, this time) and have found a way to pass stacks within javascript, so I think it will be easy to pass one back to the server. I might be able to start streaming data back and forth, which would rock.
Jeff250 wrote:You can always just use name="foo1", name="foo2", etc., and build the tuple yourself, but that's not a very satisfying solution curiosity-wise.
You're right. I'll be use forms like this from now on.

I've stopped coding on my server space for a while to study what others have been doing. Also, I've picked up eclipse and pydev, so I've found, what I think is, my favorite editor.

With javascript and python in eclipse I should be able to build a nice workspace online, to start knocking out tutorials, when time permits.
Jeff250 wrote:Also, some general code review:
* Is there a reason you're using """foo""" quotes instead of 'foo' quotes?
Oh yeah, because I want to break up the line. I hate crazy wrapping code. I can never read it.
So I can do something like:
mystring="""Once upon a time""" """ there was a rabbit""" """ named thunder."""
Jeff250 wrote: * If self.js[0] can contain arbitrary user input, then your code could be unsafe XSS-wise.
I have not forgotten your post on using cookies. I plan to spend time working on making more secure sites.

Thanks a million!
s☼-£♦и̫͍ͥ̍ͪ͌̓͗͡о̡̹̱͊̅ͮ̓̕͢б̧̝̻̪̤̳̜͐̓̉ͤ͢͜ ͙̬͙̆̑ͮ̐ͭ̾̂́͘i̎̌̾̓̽̀̈̓̀҉͉̙̦͎̘̝͕f̻͕͔̘ͣͣ̓͊̿͢͜ ͍͔͈͕̮̫ͣ̆ͮ̊͋/♂6Æ!♪╩"▲L└уͭ̂͐̇҉̴̣̼̞̠̯͓̺̞ф̜̊͌̈́̋̏̐́ц̨͔̮̿̇ ̨̛͖̙͖̖̮̗̱ͩ̆͞ͅа̥͇̞̖͚̟̅͐ͤ͞͠͠э̜̘̩̳̬͔̾ͯ̀ͫ̒̐̿ͅͅг̭̖̀ͦ̒̑ͥ̌ͮͫ͞ё͔̟̃ͬ̾̓͟ё̦̞̙̫͔̩͑̀͂ͯ̄̔̃̑̀͠ͅͅ
User avatar
Jeff250
DBB Master
DBB Master
Posts: 6387
Joined: Sun Sep 05, 1999 2:01 am
Location: ☃☃☃

Re: Question about how I'm passing a stack from js to python

Post by Jeff250 » Sun Apr 03, 2011 10:03 pm

Isaac wrote:And the android browser. I'll have to get more information, though I think I've got all that on Google Analytics. I don't get much traffic so it's easy for me sort out who's who.
I'm a luddite--I still own a flip phone. :P

The most common time people use multiple POST parameters with the same name is when they use checklists. The idiom is to give every checkbox in the same list the same name. That way your framework gives you a nice list-like data structure with all of the checked items, which is natural to work with. Giving multiple text fields the same name is more unusual. I've never done it myself, and it might be a code path that isn't well-tested on some browsers. Still, I'm suspicious that there isn't something more going on.
Isaac wrote:It's actually a single CGI file that imports a main module, which imports a sub module, then imports another submodule, and so on. So it's just one python script that grows depending on what string it's fed. It's probably a bad way of doing things.
I mean, what kind of file is your CGI script generating, i.e., does it have an XHTML doctype?
Isaac wrote:You're right. I'll be use forms like this from now on.
I still hope you're interested in getting to the bottom of this.
Isaac wrote:So I can do something like:
mystring="""Once upon a time""" """ there was a rabbit""" """ named thunder."""

Code: Select all

>>> 'a' 'b' 'c'
'abc'
:P

edit:

You also technically need a <div></div> tag (or some other block element) to wrap the input tags inside of the form, although I doubt this would cause the problem. In general though, you want to make sure that your generated html validates (validator.w3.org) before trying to debug weird issues with it.
User avatar
Jeff250
DBB Master
DBB Master
Posts: 6387
Joined: Sun Sep 05, 1999 2:01 am
Location: ☃☃☃

Re: Question about how I'm passing a stack from js to python

Post by Jeff250 » Sun Apr 03, 2011 10:06 pm

Also, I think you don't mean to say "stack" here--maybe just "list" or "array." A stack in Computer Science is a specific kind of list or array where the last thing you put in is the first thing that gets taken out (I use this data structure to decide which clothes on my floor to wear in the morning ;)).
User avatar
Isaac
DBB Artist
DBB Artist
Posts: 7127
Joined: Mon Aug 01, 2005 8:47 am
Location: Ơ̸̦͇̲̬̭̱̰͎̞͈̣͎͚̳ͬ͋̃̀̇͊͂͋͐ͦ̽ͣ̂ͥ͊̅̀̚͠ B̶͖̯͉̜̰̲̓̔͋̈́ͅ È̯ Y̪̤̼͉̠̙͝

Re: Question about how I'm passing a stack from js to python

Post by Isaac » Mon Apr 04, 2011 5:31 am

Ah, thanks. I'll remember that.
s☼-£♦и̫͍ͥ̍ͪ͌̓͗͡о̡̹̱͊̅ͮ̓̕͢б̧̝̻̪̤̳̜͐̓̉ͤ͢͜ ͙̬͙̆̑ͮ̐ͭ̾̂́͘i̎̌̾̓̽̀̈̓̀҉͉̙̦͎̘̝͕f̻͕͔̘ͣͣ̓͊̿͢͜ ͍͔͈͕̮̫ͣ̆ͮ̊͋/♂6Æ!♪╩"▲L└уͭ̂͐̇҉̴̣̼̞̠̯͓̺̞ф̜̊͌̈́̋̏̐́ц̨͔̮̿̇ ̨̛͖̙͖̖̮̗̱ͩ̆͞ͅа̥͇̞̖͚̟̅͐ͤ͞͠͠э̜̘̩̳̬͔̾ͯ̀ͫ̒̐̿ͅͅг̭̖̀ͦ̒̑ͥ̌ͮͫ͞ё͔̟̃ͬ̾̓͟ё̦̞̙̫͔̩͑̀͂ͯ̄̔̃̑̀͠ͅͅ
Post Reply