Skip to content

Instantly share code, notes, and snippets.

~^(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)*[a-z](?:[a-z0-9-]*[a-z0-9])$~
function can_host_receive_mail($host) {
if (checkdnsrr($host, 'MX')) {
return true;
}
// We try the host itself if it exists no MX records were found, as per
// RFC2821.
if (checkdnsrr($host, 'A') || checkdnsrr($host, 'AAAA')) {
$h = @fsockopen($host, 25, $errno, $errstr, 30);
if ($h) {
fclose($h);
function is_well_formed_hostname($host) {
return preg_match('~^(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)*[a-z](?:[a-z0-9-]*[a-z0-9])$~', $host);
}
function is_valid_local_part($local) {
// I'm not entirely sure about this regex, but it seems to fit was RFC2822 specifies.
return preg_match("@^[-a-z0-9!#\$%&'*+/=?^_`{|}~]+(\.[-a-z0-9!#\$%&'*+/=?^_`{|}~]+)*\$@", $local);
}
function is_well_formed_email_address($email) {
// We ignore legacy addresses and local addresses.
$parts = explode('@', $email, 2);
if (count($parts) != 2) {
return false;
if (!is_well_formed_email_address($email)) {
echo "Email address is not well-formed.\n";
} else {
list(, $host) = explode('@', $email, 2);
if (can_host_receive_mail($host)) {
echo "Host $host can receive mail.\n";
} else {
echo "Host $host can't receive mail.\n";
}
}
#!/usr/bin/env python
#
# pipedarg
# by Keith Gaughan <http://talideon.com/>
#
# The software is hereby placed in the public domain, and the author
# disclaims ownership over it and all responsibility for any damage caused
# directly or indirectly through its use. It may be freely copied and/or
# mangled, provided that altered versions have a different name and are
# not attributed to the original author.
from itertools import count
def sieve():
"""
The Sieve of Erastosthenes.
If you wanted to speed this up, your best bets are to throw in a wheel.
The heapq module's already written in C, so that's as fast as it's going
to get, though if you can think of a way to avoid quite so many calls to
heapq.heapreplace() (or prevent if from sifting the heap prematurely for
<?php
/*
* Two annoying omissions from PHP are the lack of array_all() and array_any(), which check if
* a condition represented by a callback holds for all or any of the elements in an array.
*/
function array_all(array $arr, $cb) {
foreach ($arr as $e) {
if (!call_user_func($cb, $e)) {
return false;
<?php
function php_is_so_broken() {
// Note: This appears to have been fixed somewhere in the 5.1 series,
// so no more brokenness. Hurray!
foreach (func_get_args() as $arg) {
echo "$arg ";
}
echo "\n";
}
import smtpd
import asyncore
smtpd.DebuggingServer(('127.0.0.1', 1025), None)
asyncore.loop()