Created
June 14, 2010 03:27
-
-
Save jl2/437244 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# pyget.py | |
# Copyright (c) 2010, Jeremiah LaRocco [email protected] | |
# Permission to use, copy, modify, and/or distribute this software for any | |
# purpose with or without fee is hereby granted, provided that the above | |
# copyright notice and this permission notice appear in all copies. | |
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
import socket | |
import sys | |
def main(): | |
""" | |
Get the index (/) page from a web server specified on the command line. | |
""" | |
if len(sys.argv)<2: | |
print("A network address is required") | |
exit(1) | |
theHost = sys.argv[1] | |
sep = 80*'*' | |
connected = False | |
sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
# Hosts can have multiple IP addresses, so get the list and loop through | |
# till we connect to one | |
try: | |
sai = socket.getaddrinfo(theHost, 'http') | |
except socket.gaierror as ge: | |
print("Could not find '%s': %s" % (theHost, str(ge))) | |
exit(1) | |
for adr in sai: | |
try: | |
# Try the connection | |
usedAdr = adr[4] | |
print("Trying address %s and port %d" % usedAdr) | |
sk.connect(usedAdr) | |
except socket.error as se: | |
# Normally this would probably just be 'pass' | |
print("Could not connect to %s! Trying next address" % | |
(usedAdr[0])) | |
else: | |
# If control "flowed off the end" of the try block then we've | |
# connected so break out of the loop | |
connected = True | |
break | |
if not connected: | |
# Couldn't connect to any of the hosts IP addresses, so fail | |
print("Could not connect to %s!" % (theHost)) | |
exit(1) | |
# Send the HTTP GET request | |
req = ("GET / HTTP/1.0\r\n" | |
"Accept: */*\r\n" | |
"Host: %s:%d\r\n" | |
"User-Agent: Bob\r\n" | |
# "Connection: Keep-Alive\r\n" | |
"\r\n" % (theHost, usedAdr[1])) | |
print("Request\n%s\n%s" % (sep, req)) | |
req = req.encode('utf-8') | |
# Handle partial sends | |
remaining = len(req) | |
while remaining>0: | |
remaining -= sk.send(req[len(req)-remaining:]) | |
# receive and aggregate the results | |
resp = bytes() | |
nresp = sk.recv(1024) | |
while nresp: | |
resp += nresp | |
nresp = sk.recv(1024) | |
print("Response\n%s\n%s" % (sep, resp.decode('utf-8'))) | |
sk.close() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment