All perfect praise be to Allah, The Lord of the worlds. I testify that there is none worthy of worship except Allah and that Muhammad, sallallahu ‘alayhi wa sallam, is His slave and Messenger.
It was reported in the Sunnah that it is forbidden to kill the (harmless) snake if it is inside a house until it has been warned. The Shaafi‘i scholar Ibn Hajar Al-Haytami wrote, "As for the snakes that reside inside houses, they are not to be killed until they have been warned thrice. The scholars held different views as whether the intended meaning is three days or three times. The first is the view adopted by the majority of the scholars ... The most likely correct view is that warning (the harmless house snake) is recommended, though the statements of some Hanbali scholars suggested that it is obligatory..." [Al-Fataawa Al-Fiqhiyyah Al-Kubra]
The scholars held different opinions about those house snakes and whether they are those that are found anywhere or particularly in the houses of Madeenah. An-Nawawi wrote, "Al-Maaziri said, 'The snakes in the city of the Prophet should not be killed unless they have been warned, as stated in these ahaadeeth. If they are warned but did not go away, one may kill them. As for snakes in other than Madeenah, in different parts of the world and houses and buildings, it is recommended to kill them without warning given the general indication of the authentic ahaadeeth commanding killing them.'" [Sharh Muslim]
As for the fact that these snakes may harm people and may be poisonous, if their harm is feared, then the harm must be eliminated even by killing them from the first time (without warning). If they are of a kind known to be poisonous and it is feared that they would harm people, it is allowed to kill them. Al-Qurtubi wrote about the command to warn snakes before killing them, "The command in this regard is not an obligation; if the harm is certain, it must be eliminated."
Shaykh Ibn ‘Uthaymeen said regarding killing the snake even if done in the first time, "He warns it three times by saying to it: I warn you not to be in my house, or similar words... If the snake attacked him, he has the right to ward it off, even if it leads to killing it as long as warding off its harm cannot be done except by killing it or its attack cannot be stopped except by killing it. It is allowed to kill it in this situation because it is considered self-defense."
Allah knows best.