Created
November 6, 2014 13:37
-
-
Save Sennahoi/0e38cc378611a51c67ff to your computer and use it in GitHub Desktop.
Send arbitrary data via http with python's httplib
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 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() |
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 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