Skip to content

Instantly share code, notes, and snippets.

@Sennahoi
Created November 6, 2014 13:37
Show Gist options
  • Save Sennahoi/0e38cc378611a51c67ff to your computer and use it in GitHub Desktop.
Save Sennahoi/0e38cc378611a51c67ff to your computer and use it in GitHub Desktop.
Send arbitrary data via http with python's httplib
import httplib
import urllib
DATA = "Hello"
connection = httplib.HTTPConnection("localhost:9999", timeout=20)
connection.request("POST", "/stream", headers = {'content-length' : str(len(DATA))})
connection.send(DATA)
response = connection.getresponse()
print response.read()
connection.close()
import SimpleHTTPServer
import SocketServer
PORT = 9999
DATA = "Hellooooo"
class MyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_POST(self):
length = int(self.headers["content-length"])
bytes_read = 0
while bytes_read < length:
bytes = self.rfile.read(1)
print bytes
bytes_read += 1
self.send_response(200)
self.send_header("content-length", str(len(DATA)))
self.end_headers()
self.wfile.write(DATA)
self.wfile.close();
httpd = SocketServer.TCPServer(("", PORT), MyHandler)
print "serving at port", PORT
httpd.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment