mpicard / pyvas

OpenVAS Management Protocol Client for Python
MIT License
23 stars 10 forks source link

why do I have to use with #22

Closed kounelios13 closed 5 years ago

kounelios13 commented 5 years ago

I noticed that in your examples I have to do with Client() as cli.However I tried to do the following:


cli = Client(host="localhost",username="foo",password="bar")
cli.list_tasks()

This however throws an error telling me TypeError: File or filename expected, got 'NoneType'.

Any ideas why this is happening?

ghost commented 5 years ago

I confess that I've always followed Martin's example, but I have just tried it the way you describe, with the same result as you. Martin may have more insights on this. But I therefore recommend that you do it with the "with Client() as cli" way.

ghost commented 5 years ago

I think I have a bit better understanding of this now.

Using the with-as construct, calls not only the constructor for the Client class, but also the __enter__ method to set up the connection and the __exit__ method to clean up afterwards. This doesn't happen if you don't use this construct, therefore the connection fails.

This explanation may be helpful to read in conjunction with the client.py file in pyvas (the __enter__ and __exit__ methods are right at the end of the file. http://effbot.org/zone/python-with-statement.htm

kounelios13 commented 5 years ago

@jal58 Thank you very much for the last link.It is especially interesting since I am still learning python and it helped me understand some things better

ghost commented 5 years ago

You're most welcome, and I'm glad you asked the question because I learned from it too!

Best wishes,

Anna