Can anyone help me get this up and running?

marrex25
9 years ago

0

im having some problems getting a python coded tcp server up and running i want it to record logs as well as have multiple connections

here is the code:

import logging

import SocketServer

import socket

import threading

bind_ip = “0.0.0.0”
bind_port = 9999

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

server.bind((bind_ip,bind_port))

server.listen(5)

print (“[*] Listening on %s:%d” % (bind_ip,bind_port))

# this is our client-handling thread  

def handle_client(client_socket):

# print out what the client sends  
request = client_socket.recv(1024)  
assert isinstance(request, object)  
print ("[*] Received: %s" % request)  

while True:

[client, addr] = server.accept()  

print (u"[*] Accepted connection from: {0:s}:{1:d}“.format(addr[0], addr[1]))

# spin up our client thread to handle incoming data  

client_handler = threading.Thread(target=handle_client,args=(client,))

client_handler.start()

logging.basicConfig(level=logging.DEBUG,
format=‘%(name)s: %(message)s’,
)

class EchoRequestHandler(SocketServer.BaseRequestHandler):

def __init__(self, request, client_address, server):  
    self.logger = logging.getLogger('EchoRequestHandler')  
    self.logger.debug('__init__')  
    SocketServer.BaseRequestHandler.__init__(self, request, client_address, server)  
    return  

def setup(self):  
    self.logger.debug('setup')  
    return SocketServer.BaseRequestHandler.setup(self)  

def handle(self):  
    self.logger.debug('handle')  

    # Echo the back to the client  
    data = self.request.recv(1024)  
    self.logger.debug('recv()->"%s"', data)  
    self.request.send(data)  
    return  

def finish(self):  
    self.logger.debug('finish')  
    return SocketServer.BaseRequestHandler.finish(self)  

class EchoServer(SocketServer.TCPServer):

def __init__(self, server_address, handler_class=EchoRequestHandler):  
    self.logger = logging.getLogger('EchoServer')  
    self.logger.debug('__init__')  
    SocketServer.TCPServer.__init__(self, server_address, handler_class)  
    return  

def server_activate(self):  
    self.logger.debug('server_activate')  
    SocketServer.TCPServer.server_activate(self)  
    return  

def serve_forever(self):  
    self.logger.debug('waiting for request')  
    self.logger.info('Handling requests, press <Ctrl-C> to quit')  
    while True:  
        self.handle_request()  
    return  

def handle_request(self):  
    self.logger.debug('handle_request')  
    return SocketServer.TCPServer.handle_request(self)  

def verify_request(self, request, client_address):  
    self.logger.debug('verify_request(%s, %s)', request, client_address)  
    return SocketServer.TCPServer.verify_request(self, request, client_address)  

def process_request(self, request, client_address):  
    self.logger.debug('process_request(%s, %s)', request, client_address)  
    return SocketServer.TCPServer.process_request(self, request, client_address)  

def server_close(self):  
    self.logger.debug('server_close')  
    return SocketServer.TCPServer.server_close(self)  

def finish_request(self, request, client_address):  
    self.logger.debug('finish_request(%s, %s)', request, client_address)  
    return SocketServer.TCPServer.finish_request(self, request, client_address)  

def close_request(self, request_address):  
    self.logger.debug('close_request(%s)', request_address)  
    return SocketServer.TCPServer.close_request(self, request_address)  

if name == ‘main’:
import socket
import threading

address = ('localhost', 0) # let the kernel give us a port  
server = EchoServer(address, EchoRequestHandler)  
ip, port = server.server_address # find out what port we were given  

t = threading.Thread(target=server.serve_forever)  
t.setDaemon(True) # don't hang on exit  
t.start()  

logger = logging.getLogger('client')  
logger.info('Server on %s:%s', ip, port)  

# Connect to the server  
logger.debug('creating socket')  
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  
logger.debug('connecting to server')  
s.connect((ip, port))  

# Send the data  
message = 'Hello, world'  
logger.debug('sending data: "%s"', message)  
len_sent = s.send(message)  

# Receive a response  
logger.debug('waiting for response')  
response = s.recv(len_sent)  
logger.debug('response from server: "%s"', response)  

# Clean up  
logger.debug('closing socket')  
s.close()  
logger.debug('done')  
server.socket.close()
2replies
3voices
166views
? [bolofecal]
9 years ago

0

Hint: post your code inside [code][/code]

DOSmaster
9 years ago | edited 9 years ago

0

Agreed on the hint, Python is picky on indentation so… yeah :D

Discussion thread has been locked. You can no longer add new posts.
1 of 3

This site only uses cookies that are essential for the functionality of this website. Cookies are not used for tracking or marketing purposes.

By using our site, you acknowledge that you have read and understand our Privacy Policy, and Terms of Service.

Dismiss