Diferències

Ací es mostren les diferències entre la revisió seleccionada i la versió actual de la pàgina.

Enllaç a la visualització de la comparació

Ambdós costats versió prèvia Revisió prèvia
info:cursos:pue:python-pcpp1:m4:1.6 [19/01/2024 00:51] mateinfo: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:{{ :info:cursos:pue:python-pcpp1:m4:pasted:20240119-023037.png }}
 +
 +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 ''requests'' functions are in the habit of raising an exception when they encounter any kind of communication problem, although some of the problems seem to be more common than others.
 +
 +For example, let's take a look at a crucial issue named ''timeout''.
 +
 +It's normal for the server not to respond immediately – making connections, transmitting data, searching resources – all these steps take time. Okay, we know that, but our patience is limited, too. Usually, we know very well how long we agree to wait and don't want to wait any longer. Can our expectations be met?
 +
 +Of course they can, and one exception will be very helpful – look:
 +<code python>
 +import requests
 +
 +try:
 +    reply = requests.get('http://localhost:3000', timeout=1)
 +except requests.exceptions.Timeout:
 +    print('Sorry, Mr. Impatient, you didn\'t get your data')
 +else:
 +    print('Here is your data, my Master!')
 +</code>
 +    
 +As you can see, the ''get()'' function takes one additional argument named timeout – it's the maximum time (measured in seconds and expressed as a real number) we agree to wait for a server's response. If the time is exceeded, ''get()'' will raise an exception named ''requests.exceptions.Timeout''.
 +
 +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:<code ; output>
 +Here is your data, my Master!
 +</code>
 +
 +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:<code ; output>
 +Sorry, Mr. Impatient, you didn't get your data.
 +</code>
 +
 +Of course, problems may appear much earlier, e.g., while establishing the connection:
 +<code python>
 +import requests
 +
 +try:
 +    reply = requests.get('http://localhost:3001', timeout=1)
 +except requests.exceptions.ConnectionError:
 +    print('Nobody\'s home, sorry!')
 +else:
 +    print('Everything fine!')
 +</code>
 +    
 +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
 +</code>
 +
 +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('http:////////////')
 +except requests.exceptions.InvalidURL:
 +    print('Recipient unknown!')
 +else:
 +    print('Everything fine!')
 +</code>
 +    
 +Disasters of this kind are served by an exception named:<code ; output>
 +requests.exceptions.InvalidURL
 +</code>
 +
 +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
 +|   |___ProxyError
 +|   |___SSLError
 +|___Timeout
 +|   |___ConnectTimeout
 +|   |___ReadTimeout
 +|___URLRequired
 +|___TooManyRedirects
 +|___MissingSchema
 +|___InvalidSchema
 +|___InvalidURL
 +|   |___InvalidProxyURL
 +|___InvalidHeader
 +|___ChunkedEncodingError
 +|___ContentDecodingError
 +|___StreamConsumedError
 +|___RetryError
 +|___UnrewindableBodyError
 +</code>
 +
 +Something nice for everyone.
 +
 +Now that we are well acquainted with the world of ''requests'', we can take the next step and enter the world of web services. Are you ready?
  • info/cursos/pue/python-pcpp1/m4/1.6.1705654291.txt.gz
  • Darrera modificació: 19/01/2024 00:51
  • per mate