Last active
August 29, 2015 14:07
-
-
Save kasisnu/65f52d8b0aa09d61d6aa 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/env python | |
| import argparse | |
| import socket | |
| import os | |
| parser = argparse.ArgumentParser(description='This is a socket listener. Specify port to listen on with -p. Specify file to write to with -f') | |
| #Add arguments | |
| parser.add_argument('-f', nargs='?' , help="This is the file to append to" ) | |
| parser.add_argument('-p', type=int , help="This is the port to listen on", required=False ) | |
| parser.add_argument('-u', nargs='?' , help="This is the unix socket file to work with" ) | |
| results = parser.parse_args() | |
| TCP_IP = '127.0.0.1' | |
| TCP_PORT = results.p | |
| s = socket.socket() | |
| #print results.u | |
| if results.u != None: | |
| s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) | |
| server_address = results.u | |
| try: | |
| print "Socket already exists! Unlinking old socket!" | |
| os.unlink(server_address) | |
| except OSError: | |
| pass | |
| s.bind(server_address) | |
| #s.connect(server_address) | |
| else: | |
| s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
| s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
| s.bind((TCP_IP, TCP_PORT)) | |
| print "Socket Created" | |
| print "Listening.." | |
| s.listen(1) | |
| conn, addr = s.accept() | |
| #print addr | |
| #wtf - Write to file | |
| if results.f!=None: | |
| wtf = True | |
| else: | |
| wtf = False | |
| try: | |
| while True: | |
| data = conn.recv(1024) | |
| if not data: break | |
| if wtf: | |
| with open(results.f, 'a') as outfile: | |
| outfile.write(data) | |
| else: | |
| print "received data:", data | |
| finally: | |
| conn.close() | |
| s.close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment