Skip to content

Instantly share code, notes, and snippets.

View goujonbe's full-sized avatar
👋

Benoît Goujon goujonbe

👋
View GitHub Profile
@goujonbe
goujonbe / main.go
Last active June 14, 2020 14:12
A sequential TCP port scanner.
package main
import (
"fmt"
"net"
)
func main() {
for i := 1; i <= 1024; i++ {
address := fmt.Sprintf("scanme.nmap.org:%d", i)
@goujonbe
goujonbe / main.go
Last active June 20, 2020 18:19
A TCP port scanner that uses goroutines and Channels
package main
import (
"fmt"
"net"
"sort"
)
func worker(ports, results chan int) {
for p := range ports {
@goujonbe
goujonbe / logging_example.py
Last active October 31, 2020 16:23
Gist that is part of a tutorial on pytest features. About logging testing.
def translate_animal_names(
animals: List[str],
translations: Dict[str, str]
) -> List[str]:
translated_animal_names = []
for animal in animals:
if animal in translations:
translated_animal_names.append(translations[animal])
else:
logging.warning(
@goujonbe
goujonbe / test_logging_example.py
Last active October 31, 2020 16:23
Gist that is part of a tutorial on Pytest features.
def test_translate_animal_names(caplog):
animals_in_french = ["lapin", "grenouille", "cheval"]
french_english_translation = {
"lapin": "rabbit",
"cheval": "horse"
}
animals_in_english = translate_animal_names(
animals_in_french,
french_english_translation
)
class InvalidEmailAddressError(Exception):
pass
def validate_email_address(ctx, param, value):
regex = "^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$"
if not re.search(regex, value):
raise InvalidEmailAddressError(f"Invalid email address: {value}")
return value
def test_email_validation():
with pytest.raises(InvalidEmailAddressError):
assert validate_email_address(None, None, "invalid.com")
def get_time_since_subscription(subscription_date: datetime.datetime) -> int:
current_date = datetime.datetime.utcnow()
return (current_date - subscription_date).days
@pytest.mark.freeze_time("2020-01-15 13:00:00")
def test_get_time_since_subscription():
test_subscribtion_date = datetime.datetime(2020, 1, 10, 12)
assert get_time_since_subscription(test_subscribtion_date) == 5
def is_valid_email_address(email: str) -> bool:
regex = "^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$"
return re.search(regex, email) is not None
@pytest.mark.parametrize(
"test_input, expected",
[
("correct@gmail.com", True),
("another.correct@custom.fr", True),
("and-another@custom.org", True),
("inavlid.com", False),
("veryb@d@.com", False),
("domain@too.long", False)
]