Created
October 15, 2017 11:00
-
-
Save WangYihang/c42767b4135e494c7d279aa2863820e8 to your computer and use it in GitHub Desktop.
Simple Echo Server via Python using accept
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import socket | |
def handle(connection_socket): | |
print "[+] Handling socket.." | |
connection_socket.send("Hello world!\n") | |
print "[+] Closing socket..." | |
connection_socket.shutdown(socket.SHUT_RDWR) | |
connection_socket.close() | |
def run(host, port): | |
print "[+] Starting server at %s:%d" % (host, port) | |
listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
listen_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True) | |
listen_socket.bind((host, port)) | |
listen_socket.listen(0) | |
while True: | |
connection_socket, connection_address = listen_socket.accept() | |
print "[+] Connected from %s:%d" % (connection_address[0], connection_address[1]) | |
handle(connection_socket) | |
def main(): | |
host = "127.0.0.1" | |
port = 8888 | |
run(host, port) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment