0

I am using requests in python for checking malicious headers. My code is working, but this one specific ip does not close the connection nor does it timeout.

def check():
  ...
  r=requests.head('https://50.79.153.56',verify=False,timeout=(4,4))
  ...

Using curl i found out that the site keeps sending the same sequence of bytes

curl -v -k https://50.79.153.56
....
> ~▒}#▒!}!}!} }8}"}&} } } } }#}$▒#}%}&i+G}<}'}"}(}"▒▒~~▒}#▒!}!}!} }

Using this , brings success!

curl -v -k https://50.79.153.56 -m 3
...
curl: (28) Operation timed out after 3000 milliseconds with 51 bytes received

How should the timeout flag in requests look like so similar occurrences do not happen? I tried both python2.7 and 3.x but the result is the same.

1 Answers1

0
import requests

try :
    r=requests.head('https://50.79.153.56',verify=False,timeout=4)
except :
     print("Time out Happened")

it is sample with 4 sec time out

for python2.7

    r=requests.head('https://50.79.153.56',verify=False,timeout=urllib3.Timeout(4,4))
Nozar Safari
  • 505
  • 4
  • 17
  • as you can see in my sample code , i am already using timeout=, but as i said it is not working nor is it timeouting. run the code – ling.san43 Jan 12 '20 at 12:09
  • u use touple for timeout i dont know what it mean `timeout=(4,4)` this code work i test it! even with that touple work too try reinstall `requests` – Nozar Safari Jan 12 '20 at 12:10
  • are u using `requests` or it is other module ? – Nozar Safari Jan 12 '20 at 12:11
  • python Python 2.7.16 (v2.7.16:413a49145e, Mar 4 2019, 01:30:55) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import requests >>> r=requests.head('https://50.79.153.56',verify=False,timeout=4) C:\Python27\lib\site-packages\urllib3\connectionpool.py:851: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings InsecureRequestWarning) it just hangs – ling.san43 Jan 12 '20 at 12:15
  • ok your pyrhon is 2.7 mention it in question . it is python 3.x code and u need `pip install requests` can u change python version or u must use 2.7? – Nozar Safari Jan 12 '20 at 12:20
  • Though I don't know why yet, I have the same behavior on Python3: it throws an InsecureRequestWarning error, and never time-outs; but if you change to verify=True, it time-outs after 4 sec. – Kalma Jan 12 '20 at 12:24
  • look at link address `https://..` SSL need verify . u can't set it False , i edited answer for python2.7 too – Nozar Safari Jan 12 '20 at 12:26
  • Yes, I figured out now. Time-out clause is for a non-successful connection. It will not time-out when connected and receiving data. To do that, I guess you should control the total connection time by other means (e.g. using a thread which makes the connection, and killing it after a specific amount of time) – Kalma Jan 12 '20 at 12:35
  • @Kalma so this is expected behavior ? – ling.san43 Jan 12 '20 at 12:37
  • Yes. But you can control the total connection time (or size) which is what you need: Take a look to this: https://stackoverflow.com/questions/22346158/python-requests-how-to-limit-received-size-transfer-rate-and-or-total-time – Kalma Jan 12 '20 at 12:48