Diferències
Ací es mostren les diferències entre la revisió seleccionada i la versió actual de la pàgina.
Ambdós costats versió prèvia Revisió prèvia | |||
info:cursos:pue:python-pcpp1:m4:1.6 [19/01/2024 00:51] – mate | info:cursos:pue:python-pcpp1:m4:1.6 [19/01/2024 02:37] (actual) – mate | ||
---|---|---|---|
Línia 176: | Línia 176: | ||
We aren't going to discuss them here, but feel free to deepen your knowledge by yourself. | We aren't going to discuss them here, but feel free to deepen your knowledge by yourself. | ||
+ | As you probably suspect, all the listed HTTP methods have their reflections (or rather siblings) within the requests module. | ||
+ | |||
+ | Yes, you're absolutely right:{{ : | ||
+ | |||
+ | The diagram, although extremely simplified and deprived of important details, shows us the most important tools we’ll use soon to play a game with our server. Don't be afraid, we'll acquaint you with all additional means and arguments. | ||
+ | |||
+ | |||
+ | It seems that we missed one important issue: what’ll happen if **anything goes wrong**? The server may fail, transmission media may be down, etc, etc, etc. How do we defend against all these miseries? | ||
+ | |||
+ | All '' | ||
+ | |||
+ | For example, let's take a look at a crucial issue named '' | ||
+ | |||
+ | It's normal for the server not to respond immediately – making connections, | ||
+ | |||
+ | Of course they can, and one exception will be very helpful – look: | ||
+ | <code python> | ||
+ | import requests | ||
+ | |||
+ | try: | ||
+ | reply = requests.get(' | ||
+ | except requests.exceptions.Timeout: | ||
+ | print(' | ||
+ | else: | ||
+ | print(' | ||
+ | </ | ||
+ | | ||
+ | As you can see, the '' | ||
+ | |||
+ | If the server is ready and not very busy, one second is more than enough to process such a simple request, so you should expect good news – the program will write:< | ||
+ | Here is your data, my Master! | ||
+ | </ | ||
+ | |||
+ | But if you change the timeout radically to a disturbingly small value like 0.00001, it's highly probable that you will have to endure the following bad news:< | ||
+ | Sorry, Mr. Impatient, you didn't get your data. | ||
+ | </ | ||
+ | |||
+ | Of course, problems may appear much earlier, e.g., while establishing the connection: | ||
+ | <code python> | ||
+ | import requests | ||
+ | |||
+ | try: | ||
+ | reply = requests.get(' | ||
+ | except requests.exceptions.ConnectionError: | ||
+ | print(' | ||
+ | else: | ||
+ | print(' | ||
+ | </ | ||
+ | | ||
+ | This code has no chance of running properly – it’s addressing its efforts to port 3001, while our server is listening at port 3000. No helping hand will fix this misunderstanding – client and server won't meet and an exception will be raised. Its name is:<code ; output> | ||
+ | requests.exceptions.ConnectionError | ||
+ | </ | ||
+ | |||
+ | To err is human, so it is also possible that you or another developer may leave the resource’s URI in a somewhat malformed state. Look at the code in the editor window. | ||
+ | |||
+ | <code python> | ||
+ | import requests | ||
+ | |||
+ | try: | ||
+ | reply = requests.get(' | ||
+ | except requests.exceptions.InvalidURL: | ||
+ | print(' | ||
+ | else: | ||
+ | print(' | ||
+ | </ | ||
+ | | ||
+ | Disasters of this kind are served by an exception named:< | ||
+ | requests.exceptions.InvalidURL | ||
+ | </ | ||
+ | |||
+ | We’ve gathered all the requests exceptions in one place and presented them as a tree – this is what it looks like: | ||
+ | <code ; output> | ||
+ | RequestException | ||
+ | |___HTTPError | ||
+ | |___ConnectionError | ||
+ | | | ||
+ | | | ||
+ | |___Timeout | ||
+ | | | ||
+ | | | ||
+ | |___URLRequired | ||
+ | |___TooManyRedirects | ||
+ | |___MissingSchema | ||
+ | |___InvalidSchema | ||
+ | |___InvalidURL | ||
+ | | | ||
+ | |___InvalidHeader | ||
+ | |___ChunkedEncodingError | ||
+ | |___ContentDecodingError | ||
+ | |___StreamConsumedError | ||
+ | |___RetryError | ||
+ | |___UnrewindableBodyError | ||
+ | </ | ||
+ | |||
+ | Something nice for everyone. | ||
+ | |||
+ | Now that we are well acquainted with the world of '' |