I've googled many articles, but still without significant information.
My question is about how to stop asyncore.loop() in client side.
My code is the following
import socket, asyncore
class Client(asyncore.dispatcher):
def __init__(self, host, port):
self.buffer = ''
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.connect((host, port))
def writable(self):
return len(self.buffer) > 0
def handle_connect(self):
print "handle_connect"
def handle_close(self):
print "handle_close"
self.close()
def handle_write(self):
print "handle_write"
sent = self.send(self.buffer)
self.buffer = self.buffer[sent:]
print sent
def command(self, data):
self.buffer = data
def close(self):
self.close()
mysocket = Client("127.0.0.1", 8888)
mysocket.command("asfas\n")
asyncore.loop()
mysocket.close()
Thanks in advance!
Related
I have a boolean variable:
is_conected = False
And I have this part of code:
def start(self, host='localhost', port=32767):
self.__connection.bind((host, port))
self.__connection.listen(5)
def send_frame(self, frame: str):
if not self.__connection.send(bytes(frame, encoding='utf-8')):
raise RuntimeError("Connection broken.")
def recieve_frame(self) -> str:
socket, adress = self.__connection.accept()
str(__data) = socket.recv(1024)
__frame_buffer.append(__data)
Where I need to put If... Else, for checking, if someone connected or not? And what kind of condition I need to use?
The whole code:
import socket
class TCPServer():
__frame_buffer = list()
__connection = None
is_conected = True
def __new__(cls):
if not hasattr(cls, 'instance'):
cls.instance = super(TCPServer, cls).__new__(cls)
return cls.instance
def __init__(self):
self.__connection = socket.socket(
socket.AF_INET, socket.SOCK_STREAM)
def start(self, host='localhost', port=32767):
# """ Start server """
self.__connection.bind((host, port))
self.__connection.listen(5)
def send_frame(self, frame: str):
# """ Send frame """
if not self.__connection.send(bytes(frame, encoding='utf-8')):
raise RuntimeError("Connection broken.")
def recieve_frame(self) -> str:
socket, adress = self.__connection.accept()
str(__data) = socket.recv(1024)
__frame_buffer.append(__data)
def get_frame(self) -> str:
if self.__frame_buffer:
self.__frame_buffer.pop(0)
server = TCPServer()
while True:
if server.is_conected:
server.send_frame(str(input("String to send")))
print(server.get_frame())
That's the whole code. I don't write a more part of it, only a send and recieve frame function. So, that's kinda a school project for me and I'm not too good in working with any kind of connection or protocols
This is how the server generally works
with socket(AF_INET, SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen() # Start listening
while True:
conn, addr = s.accept() # Accept connection request
Connected = True # You are connected
try:
#Work with connection
except:
Connected = False
Figure out how you can do this with your code
I have a socketserver in Python which has to handle multiple clients using the select.select method, as seen in the code below:
import socket
import select
class Server:
def __init__(self):
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server.bind(('localhost', 2000))
self.socket_list = [self.server]
self.addresses = ['localhost']
self.commands = [""]
self.running = True
self.server.listen(10)
def listen(self):
while self.running:
read, write, error = select.select(self.socket_list, [], self.socket_list, 0)
for sock in read:
if sock == self.server and self.running:
try:
conn, address = self.server.accept()
conn.settimeout(30)
self.socket_list.append(conn)
self.addresses.append(address[0])
self.commands.append("")
except:
self.shutdown()
break
elif self.running:
try:
packet = sock.recv(60)
if not packet:
self.close_conn(sock)
index = self.socket_list.index(sock)
self.commands[index] += packet
if '\n' in self.commands[index]:
#handle command
except:
self.close_conn(sock)
def close_conn(self, conn):
#close client conn
def shutdown(self):
#shutdown server
if __name__ == "__main__":
Server().listen()
The problem i currently have is that the client's connection should be closed after 30 seconds, but that doesn't happen, even though i declared that by using conn.settimeout(30). I haven't found any explanation yet as to why this happens.
Note: comments were used to replace parts of the code that didn't mater to the problem.
Please look at the following python code.
I created a Server class to listen on port 10000 to receive UDP broadcast messages.
If IP address is configured in the system, it can receive UDP broadcast messages. If no ip address configured, it cannot receive any messages.
Could you tell me why?
import socket
import sys
class Server:
class Handler:
def handle(self, message):
pass
def __init__(self, serialNo):
self.serialNo = serialNo
def _setAddress(self, socket, message, address):
self.message = message
self.address = address
self.socket = socket
def send(self, message):
self.socket.sendto(message, self.address)
def getSerialNo(self):
return self.serialNo
def __init__(self, port, handler):
self.ss = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.ss.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.ss.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
self.handler = handler
try:
self.ss.bind(('<broadcast>', port))
except:
self.ss.close()
raise RuntimeError("Create socket error")
self.ss.setblocking(1)
def loop(self):
while True:
try:
print "Listening for broadcast..."
message, address = self.ss.recvfrom(8192)
print "Got request from %s:%s" % (address, message)
self.handler._setAddress(self.ss, message, address)
self.handler.handle(message)
except (KeyboardInterrupt, SystemExit):
raise
except:
sys.exc_info()[0]
After referring to pydhcp client code, I made following changes:
import socket
import sys
import select
class Server:
class Handler:
def handle(self, message):
pass
def __init__(self, serialNo):
self.serialNo = serialNo
def _setAddress(self, socket, message, address):
self.message = message
self.address = address
self.socket = socket
def send(self, message):
self.socket.sendto(message, self.address)
def getSerialNo(self):
return self.serialNo
def __init__(self, port, handler):
self.ss = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.ss.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.ss.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
self.handler = handler
try:
self.ss.bind(("0.0.0.0", int(port)))
except:
self.ss.close()
raise RuntimeError("Create socket error")
def loop(self):
while True:
try:
print "Listening for broadcast..."
data_input,data_output,data_except = select.select([self.ss],[],[], 60)
if (data_input != []):
(message, address) = self.ss.recvfrom(2048)
print "Got request from %s:%s" % (address, message)
self.handler._setAddress(self.ss, message, address)
self.handler.handle(message)
else:
print "no data within 60 seconds"
except (KeyboardInterrupt, SystemExit):
raise
except:
sys.exc_info()[0]
Now it can receive the broadcasting packets, but it cannot work on RedHat.
I want to run a server which accepts connections from a few clients and also want it to send a message to a specific another server -which listen 1234 port- in init time.
self.connect(('localhost', 1234))
self.buffer = 'connect'
I put it in init method. And
made up that kind of code which is below.
class EchoHandler(asyncore.dispatcher_with_send):
def handle_read(self):
data = self.recv(8192)
if data:
print data
self.send(data + " echo")
class EchoServer(asyncore.dispatcher):
def __init__(self, host, port):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.set_reuse_addr()
self.bind((host, port))
self.listen(5)
self.connect(('localhost', 1234))
self.buffer = 'connect'
def handle_accept(self):
pair = self.accept()
if pair is not None:
sock, addr = pair
print 'Incoming connection from %s' % repr(addr)
handler = EchoHandler(sock)
def handle_write(self):
sent = self.send(self.buffer)
self.buffer = self.buffer[sent:]
server = EchoServer('localhost', 0)
asyncore.loop()
I need your help.. Looking forward your replies. Thank you..
I modified the code (found here) a bit (added class AsyncEventLoop)
import socket,asyncore
import threading
class forwarder(asyncore.dispatcher):
def __init__(self, ip, port, remoteip,remoteport,backlog=5):
asyncore.dispatcher.__init__(self)
self.remoteip=remoteip
self.remoteport=remoteport
self.create_socket(socket.AF_INET,socket.SOCK_STREAM)
self.set_reuse_addr()
self.bind((ip,port))
self.listen(backlog)
def handle_accept(self):
conn, addr = self.accept()
# print '--- Connect --- '
sender(receiver(conn),self.remoteip,self.remoteport)
def closef(self):
self.close()
class receiver(asyncore.dispatcher):
def __init__(self,conn):
asyncore.dispatcher.__init__(self,conn)
self.from_remote_buffer=''
self.to_remote_buffer=''
self.sender=None
def handle_connect(self):
pass
def handle_read(self):
read = self.recv(4096)
# print '%04i -->'%len(read)
self.from_remote_buffer += read
def writable(self):
return (len(self.to_remote_buffer) > 0)
def handle_write(self):
sent = self.send(self.to_remote_buffer)
# print '%04i <--'%sent
self.to_remote_buffer = self.to_remote_buffer[sent:]
def handle_close(self):
self.close()
if self.sender:
self.sender.close()
class sender(asyncore.dispatcher):
def __init__(self, receiver, remoteaddr,remoteport):
asyncore.dispatcher.__init__(self)
self.receiver=receiver
receiver.sender=self
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.connect((remoteaddr, remoteport))
def handle_connect(self):
pass
def handle_read(self):
read = self.recv(4096)
# print '<-- %04i'%len(read)
self.receiver.to_remote_buffer += read
def writable(self):
return (len(self.receiver.from_remote_buffer) > 0)
def handle_write(self):
sent = self.send(self.receiver.from_remote_buffer)
# print '--> %04i'%sent
self.receiver.from_remote_buffer = self.receiver.from_remote_buffer[sent:]
def handle_close(self):
self.close()
self.receiver.close()
class AsyncEventLoop(threading.Thread):
def run(self):
asyncore.loop()
starting in the code
ser = forwarder('127.0.0.1', 7774, 'google.com.ua', 80)
evLoop = AsyncEventLoop()
Can I change remoteip, remoteport on the fly? If the fly is not possible to change, as well stop this thread to start with the new settings (remoteip, remoteport)?
yes, you can
ser.remoteip, ser.remoteport = IP, port