Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # Cross platforrm solution for 2016 CBU EGR226 HW7
- # by Christopher Chen
- import socket
- def get_temp():
- host = "www.surf-forecast.com"
- target = "/breaks/Santa-Monic-Pier/seatemp"
- request_header = "GET {target} HTTP/1.1\nHost: {host}\n\n".format(
- target=target, host=host).encode()
- # socket stuff
- s = socket.socket(
- socket.AF_INET, socket.SOCK_STREAM)
- s.connect((host, 80))
- s.send(request_header)
- buf = b""
- res = s.recv(1024)
- # this is the delimiter,
- # the presence of which lets us know when to stop listening
- # The input is a bytestring so this must be as well
- tail = b"</html>"
- # get the whole page and put it in buf
- while len(res) > 0 and not tail in buf:
- res = s.recv(1014)
- buf += res
- # thank goodness the HTML is neatly separated onto separate lines
- # simply filter for lines for ones containing the temperature bit
- # then filter for numerical characters (and periods) in that line.
- found = float(
- ''.join(filter(
- lambda c: c in "0987654321.",
- [l.strip() for l in ''.join(map(chr, buf)).split("\n")
- if "<b>Today's Santa Monica Pier sea temperature is" in l][0],
- )
- )[:-1]
- )
- # algorithm:
- # find the line that contains the sentence about the temp.
- # filter that line for numerals and periods
- # slice off the trailing period that I know is there
- # convert the remainder to float
- s.close()
- return found
- def main():
- print("{} C".format(get_temp()))
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment