Example #1
0
def get_weather(zipcode):
	http = ClientSocket('www.weather.com', 80);
	http.send('GET /weather/local/%s HTTP/1.0\r\n' % zipcode);
	http.send('Host: www.weather.com\r\n');
	http.send('Referer: http://www.weather.com/\r\n');
	http.send('Accept: */*\r\n\r\n');

	city = zipcode;

	while True:
		try:
			(iw, ow, ew) = select.select([http], [], [], 1);
		except ValueError:
			break;

		if iw is None:
			break;

		html = str(http.read());

		t_index = html.find('<TITLE>Local Weather Forecast for ');
		if t_index != -1:
			city_i = html.find('(%s)' % zipcode);
			city = html[t_index + 34:city_i];

		temp_i = html.find('<B CLASS=obsTempTextA>');
		temp_i2 = html.find('&deg;F</B>');

		if temp_i != -1 and temp_i2 != -1:
			temp = html[temp_i + 22:temp_i2];
			break;

	http.close();
	return "It is currently %sF in %s" % (temp, city);