Re: Python Client Rest API Invocation - POST with empty body - Invalid character found in method name [{}POST]. HTTP method names must be tokens

2020-11-21 Thread Peter J. Holzer
On 2020-11-19 15:12:39 +, Shelke, Bhushan wrote:
> I have a Tomcat+Java based server exposing REST APIs. I am writing a
> client in python to consume those APIs. Everything is fine until I
> send empty body in POST request. It is a valid use case for us. If I
> send empty body I get 400 bad request error - Invalid character found
> in method name [{}POST]. HTTP method names must be tokens.
> 
> If I send empty request from POSTMAN or Java or CURL it works fine,
> problem is only when I used python as a client.
> 
> Following is python snippet -
> 
> json_object={}
> 
> header = {'alias': 'A', 'Content-Type' : 'application/json', 'Content-Length' 
> : '0'}

'Content-Length' : '0' is wrong, but it seems that requests.post
overrides that, so it doesn't matter.


> resp = requests.post(url, auth=(username, password), headers=header, 
> json=json_object)
> 
> 
> 
> I tried using data as well instead of json param to send payload with
> not much of success.
> 
> I captured the wireshark dumps to understand it further and found that
> the request tomcat received is not as per RFC2616
> (https://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html). Especially
> the part -
> 
> Request-Line = Method SP Request-URI SP HTTP-Version CRLF
> 
> I could see in from wireshark dumps it looked like - {}POST
>  HTTP/1.1
> 
> As we can see the empty body is getting prefixed with http-method,
> hence tomcat reports that as an error. I then looked at python http
> library code - client.py.

I cannot reproduce this. Using Python 3.8.2 and requests 2.22.0 (as
included in Ubuntu 20.04), the request sent is "POST /env HTTP/1.1", not
"{}POST /env HTTP/1.1",


In wireshark "Follow TCP connection", the "{}" at the end of the request
is squished together with the "HTTP/1.1 302 Found" at the beginning of
the response, but they are in different colors so that shouldn't be a
problem unless you are visually impaired, and it doesn't match your
description.

hp

-- 
   _  | Peter J. Holzer| Story must make more sense than reality.
|_|_) ||
| |   | [email protected] |-- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |   challenge!"


signature.asc
Description: PGP signature
-- 
https://mail.python.org/mailman/listinfo/python-list


Cannot update parso

2020-11-21 Thread Cecil Westerhof via Python-list
I cannot update parso because of the rule from jedi:
<0.8.0,>=0.7.0

This kind of things happens more often, but normally in less of a week
the module that prevents another to be installed is updated and the
restriction is lifted.

But I think it is about half a year that this restriction is active.
Why is jedi not updated.

(I cannot uninstall jedi, because it is used by ipython.)

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
-- 
https://mail.python.org/mailman/listinfo/python-list


Automatically advancing a bi-directional generator to the point of accepting a non-None value?

2020-11-21 Thread Go Luhng
Suppose we write a very simple bi-directional generator in Python:

def share_of_total():
s = 0
new_num = 0
while True:
new_num = yield new_num / (s or 1)
s += new_num

share_calculator = share_of_total()
next(share_calculator)  # Without this we get the TypeError

for num in [1, 2, 3]:
print(share_calculator.send(num))

This generator just accepts a number and yields a float representing
its share of the sum of all previously provided numbers.

We would ideally like to just use it immediately as follows:

share_calculator = share_of_total()
print(share_calculator.send(num))

However, this causes `TypeError: can't send non-None value to a
just-started generator`. All users of the `share_of_total()` must
remember to execute `next(share_calculator)` before the generator is
usable as intended.

Is there an elegant way to make `share_calculator` immediately usable
- i.e. to be able to immediately call `share_calculator.send(num)`
after creating `share_calculator`?

I know it can be done with a fairly trivial wrapper, but I was hoping
for a more elegant solution that doesn't require boilerplate.
-- 
https://mail.python.org/mailman/listinfo/python-list


Problem exiting from a script using tkinter

2020-11-21 Thread Paulo da Silva
Hi!

Why this does not work?!

from tkinter import *

def terminate(root):
root.quit


root=Tk()
#b=Button(root,text="QUIT",command=root.quit)
b=Button(root,text="QUIT",command=lambda: terminate(root))
b.pack()

mainloop()

Thanks
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Problem exiting from a script using tkinter

2020-11-21 Thread Chris Angelico
On Sun, Nov 22, 2020 at 9:16 AM Paulo da Silva
 wrote:
>
> Hi!
>
> Why this does not work?!
>
> from tkinter import *
>
> def terminate(root):
> root.quit
>

Is root.quit a function? Simply referencing a function's name does not
call it (because functions are first-class objects - you can put a
function in a variable or pass it as a parameter etc). In order to
make it do its work, you have to call it.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Problem exiting from a script using tkinter

2020-11-21 Thread Paulo da Silva
Às 22:18 de 21/11/20, Chris Angelico escreveu:
> On Sun, Nov 22, 2020 at 9:16 AM Paulo da Silva
>  wrote:
>>
>> Hi!
>>
>> Why this does not work?!
>>
>> from tkinter import *
>>
>> def terminate(root):
>> root.quit
>>
> 
> Is root.quit a function? Simply referencing a function's name does not
> call it (because functions are first-class objects - you can put a
> function in a variable or pass it as a parameter etc). In order to
> make it do its work, you have to call it.
> 

A newbie Error :-(
Sorry. I am giving my first steps in tkinter and I thought it was
another problem :-)
I just copied the "root.quit" inside the function.

Thanks Chris.

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


Re: Problem exiting from a script using tkinter

2020-11-21 Thread Chris Angelico
On Sun, Nov 22, 2020 at 9:36 AM Paulo da Silva
 wrote:
>
> Às 22:18 de 21/11/20, Chris Angelico escreveu:
> > On Sun, Nov 22, 2020 at 9:16 AM Paulo da Silva
> >  wrote:
> >>
> >> Hi!
> >>
> >> Why this does not work?!
> >>
> >> from tkinter import *
> >>
> >> def terminate(root):
> >> root.quit
> >>
> >
> > Is root.quit a function? Simply referencing a function's name does not
> > call it (because functions are first-class objects - you can put a
> > function in a variable or pass it as a parameter etc). In order to
> > make it do its work, you have to call it.
> >
>
> A newbie Error :-(
> Sorry. I am giving my first steps in tkinter and I thought it was
> another problem :-)
> I just copied the "root.quit" inside the function.
>

No need to feel bad about it :) Getting your head around "this is a
function, that's where the function's being called" is not easy, and
it takes experience.

Your question was very well put. You provided a short, compact example
that showcased the problem you were experiencing. That made it easy to
answer your question. Keep on asking questions like that, please -
you're helping yourself and making the mailing list a great place too
:)

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list