Last active
August 29, 2015 14:16
-
-
Save commiebstrd/13eff8f10ac3e41704bc to your computer and use it in GitHub Desktop.
Python http
This file contains 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
import socket | |
class my_sock(): | |
def __init__(self): | |
self.host = "" # hostname\IP | |
self.port = 0 # port | |
self.af_inet = socket.AF_INET # sock type | |
self.proto = socket.SOCK_STREAM # sock proto | |
self.backlog = 5 # listner backlog | |
self.size = 4096 # send\recv size | |
self.rdata = "" # storage for remaining recv data | |
self.terminator = "" # internal command termination string | |
self._is_open = False # is this socket still open | |
self._is_bind = False # is this socket bound | |
self._has_client = False # if bound, is this a connection to a client | |
self._input = [] # internal select() input list | |
self._output = [] # internal select() output list | |
self._error = [] # internal select() error list | |
self._sock = None # socket storage | |
def __del__(self): | |
if self._sock: | |
self._sock.close() | |
def connect(self): | |
try: | |
self._sock = socket.socket(self.af_inet, self.proto) | |
if socket.SOCK_STREAM == self.proto: | |
self._sock.connect((self.host,self.port)) | |
except socket.error, (value,reason): | |
if self._sock: | |
self._sock.close() | |
print "Could not open socket: " + reason | |
return False | |
self._is_open = True | |
return True | |
def recv(self, size=None): | |
if size is None: | |
size = self.size | |
tlen = len(self.terminator) | |
tmpdata = "" | |
retdata = self.rdata # retrieve anything sent from last recv | |
done = False | |
while not done: | |
try: | |
if socket.SOCK_STREAM == self.proto: | |
tmpdata = self._sock.recv(size) | |
else: | |
tmpdata, addr = self._sock.recvfrom(size) | |
except socket.error, (value,reason): | |
print "Failed to recv from " + self.host + ", is closed: ", reason | |
self._is_open = False | |
return | |
except socket.timeout, (value,reason): | |
print "Failed to recv from " + self.host + ", has timedout: " + reason | |
self._is_open = False | |
return | |
if not tmpdata: | |
print "Failed to recv from " + self.host + ", was closed or did not send anything." | |
if socket.SOCK_STREAM == self.proto: | |
self._is_open = False | |
return | |
if 0 != tlen: | |
end = tmpdata.find(self.terminator) | |
else: | |
end = 0 | |
if 0 > end: | |
# Not the end | |
retdata = retdata + tmpdata | |
else: | |
# Found terminator or none specified | |
done = True | |
self.rdata = tmpdata[end+tlen:] # store any remaining after terminator in self | |
return retdata + tmpdata[:end+tlen] # return all data until and including terminator | |
def send(self, msg=""): | |
tlen = len(self.terminator) | |
mlen = len(msg) | |
spos = 0 | |
done = False | |
while not done: | |
if 0 != tlen and buff: | |
epos = buff.find(self.terminator) + tlen | |
else: | |
epos = mlen | |
if mlen == 0 or mlen == epos: | |
# Single send (terminator at eol) | |
done = True | |
buff = msg[spos:epos] | |
#Send data | |
try: | |
if socket.SOCK_STREAM == self.proto: | |
spos = self._sock.send(buff) | |
else: | |
spos = spos + sock.sock.sendto(buff,(self.host,self.port)) | |
except socket.error, (value,reason): | |
print "Failed sending to " + self.host + ", has closed: " + reason | |
self._is_open = False | |
return False | |
except socket.timeout, (value,reason): | |
print "Failed sending to " + self.host + ", has timedout: " + reason | |
self._is_open = False | |
return False | |
if 0 == spos: | |
print "Failed sending to " + self.host + ", has closed: No data sent" | |
if socket.SOCK_STREAM == self.proto: | |
self._is_open = False | |
done = True | |
elif mlen == spos: | |
done = True | |
return True | |
def doesnt_work(): | |
sock = my_sock() | |
sock.host = "www.google.com" | |
sock.port = 80 | |
sendstr = "GET / HTTP/1.1\r\nHost: google.com\r\n\r\n" | |
# Does not work | |
if not sock.connect(): | |
return False | |
if not sock.send(sendstr): | |
return False | |
response = sock.recv() | |
print response | |
return True | |
def does_work(): | |
sock = my_sock() | |
sock.host = "www.google.com" | |
sock.port = 80 | |
# Does work | |
if not sock.connect(): | |
return False | |
if not sock.send("GET / HTTP/1.1\r\nHost: google.com\r\n\r\n"): | |
return False | |
response = sock.recv() | |
print response | |
return True | |
if does_work(): | |
print "Does_work worked" | |
else: | |
print "Does_work failed" | |
if doesnt_work(): | |
print "Doesnt_work worked" | |
else: | |
print "Doesnt_work failed" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment