How to break an infinite loop waiting to accept TCP connection with keyboard interrupt in python - tcp

i have a TCP server which is waiting for connections inside an infinite while loop. The problem is i guess as soon as it encounters accept() method it goes to sleep until there is a connection to accept. I want to break out of the loop with a keyboard interrupt but i guess as the function is waiting for connection it just ignores the interrupt..
def start_main_server(self):
print("The Server is ready to recieve")
socket_threads = list()
count = 0
while 1:
try:
connectionSocket, addr = self.serversocket.accept()
print("accepted")
connectionSocket.send(json.dumps(self.users).encode())
socket_threads.append(Thread(target=self.login,args=(connectionSocket,)))
socket_threads[count].start()
count += 1
except KeyboardInterrupt:
break
Executable code:
# coding=utf-8
from socket import *
port = 12000
servername = "127.0.0.1"
connection =socket(AF_INET,SOCK_STREAM)
connection.bind((servername,port))
connection.listen(1)
print("SERVER STARTED")
while 1:
try:
x = connection.accept()
except KeyboardInterrupt:
print("Keyboard Interrupt Occured")
break
print("I am out of loop")

Related

How can I listen multiple ports via Elixir?

I'd like to listen 2 ports via Elixir. I managed to listen the ports, though. However, I can't get data from second port.
def accept() do
{:ok, socket} = :gen_tcp.listen(7777, [:binary, packet: 0, active: false, reuseaddr: true])
{:ok, httpSocket} =
:gen_tcp.listen(8787, [:binary, packet: 0, active: false, reuseaddr: true])
http_loop_acceptor(httpSocket)
loop_acceptor(socket)
end
defp http_loop_acceptor(socket) do
{:ok, client} = :gen_tcp.accept(socket)
pid = spawn(fn -> http_serve(client) end)
:ok = :gen_tcp.controlling_process(client, pid)
http_loop_acceptor(socket)
end
defp loop_acceptor(socket) do
{:ok, client} = :gen_tcp.accept(socket)
pid = spawn(fn -> serve(client) end)
:ok = :gen_tcp.controlling_process(client, pid)
loop_acceptor(socket)
end
I can send the data to 8787 port (httpSocket). However, I can't send any data to 7777 (socket).
If change the order of these 2 lines then I can send the data to 7777 (socket), I can't send any data to 8787 port.
http_loop_acceptor(httpSocket)
loop_acceptor(socket)
How can I listen multiple ports and receive data via those ports?
In your accept function, the call to http_loop_acceptor will recurse infinitely, which means that loop_acceptor is never called.
If you want to listen on two sockets, you need to start two separate processes, one for each socket. A quick and dirty way is to use spawn, but in a real application you would model these processes as part of your supervision tree.

Tornado receive UDP packets from multicast group

I have a server where I want to receive data from multicast group. Is there any inbuilt function that I can use to receive this multicast UDP packets?
Edit: Code implementation
I have implemented the code and that follows like this:
#!/usr/bin/env python
import socket
import struct
import os
import errno
import binascii
import tornado.ioloop
from tornado.ioloop import IOLoop
from tornado.platform.auto import set_close_exec
class UDPHandler():
"""
Connect to multicast group
"""
def __init__(self, ip, port, io_loop):
self.io_loop = io_loop
self._multiIP = ip
self.port = port
self._sock = None
self._socket = {} # fd -> socket object
def conn(self):
"""
Listner to multicast group
"""
self._sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self._sock.settimeout(3)
self._sock.bind(('', self.port))
self._sock.setblocking(0)
group = socket.inet_aton(self._multiIP)
mreq = struct.pack('4sL', group, socket.INADDR_ANY)
self._sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
self._socket[self._sock.fileno()] = self._sock
print("self._sock:", self._sock)
def onRx(self, data, addr):
print("addr, data:", addr, len(str(data)))
print(data)
def r(self):
self.conn()
add_socket_handler(self._sock, self.onRx, self.io_loop)
def add_socket_handler(sock, callback, io_loop):
def accept_handler(fd, events):
while True:
try:
data, address = sock.recvfrom(1024)
except socket.error as e:
if e.args[0] in (errno.EWOULDBLOCK, errno.EAGAIN):
callback(None, None)
except Exception as e:
print("except:", e)
callback(None, None)
callback(data, address)
io_loop.add_handler(sock.fileno(), accept_handler, IOLoop.READ)
def periodic():
# print("periodic")
None
def main():
MULTICAST_IP = "224.1.1.10"
RECEIVE_PORT = 10003
udpRx = UDPHandler(MULTICAST_IP, RECEIVE_PORT, tornado.ioloop.IOLoop.current())
udpRx.r()
tornado.ioloop.PeriodicCallback(periodic, 1000).start()
tornado.ioloop.IOLoop.current().start()
if __name__ == "__main__":
main()
Now the problem is is am getting same packet in a loop even if I receive one packet I am receiving the same packet over and over again. Is there something wrong with the code? Especially with add_socket_handler?
Edit 2:
I have added a break statement in the while loop that I had in add_socket_handler and now it seems to be working good.
def add_socket_handler(sock, callback, io_loop):
def accept_handler(fd, events):
while True:
try:
data, address = sock.recvfrom(1024)
callback(data, address)
except socket.error as e:
if e.args[0] in (errno.EWOULDBLOCK, errno.EAGAIN):
raise
except Exception as e:
raise
break ## change in here
io_loop.add_handler(sock.fileno(), accept_handler, io_loop.READ)
Is this how it is suppose to be done ?
The break in your add_socket_handler looks backwards. You want to loop until you get EWOULDBLOCK/EAGAIN. (with the break as written, it will still work, but it will be slightly less efficient and might miss packets).
def add_socket_handler(sock, callback, io_loop):
def read_handler(fd, events):
while True:
try:
data, address = sock.recvfrom(1024)
callback(data, address):
except socket.error as e:
if e.errno in (errno.EWOULDBLOCK, errno.EAGAIN):
return
raise
io_loop.add_handler(sock, read_handler, io_loop.READ)
Other than that, this looks right, although I haven't worked with multicast UDP myself.

Operating Micropython-running WeMos D1 mini (ESP8266) pins with HTTP requests

What I am trying to ultimately achieve is to control my garage door opener with a relay connected to a WeMos D1 Mini, connected to my home WiFi. I am using the openGarageDoor() function. Everything works fine with serial connection.
I have been trying to run HTTP server on a WeMos D1 Mini with this script.
customertagsAction() -- try:
import usocket as socket
except:
import socket
CONTENT = b"""\
HTTP/1.0 200 OK
Hello #%d from MicroPython!
"""
def main(micropython_optimize=False):
s = socket.socket()
# Binding to all interfaces - server will be accessible to other hosts!
ai = socket.getaddrinfo("0.0.0.0", 8080)
print("Bind address info:", ai)
addr = ai[0][-1]
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(addr)
s.listen(5)
print("Listening, connect your browser to http://<this_host>:8080/")
counter = 0
while True:
res = s.accept()
client_sock = res[0]
client_addr = res[1]
print("Client address:", client_addr)
print("Client socket:", client_sock)
if not micropython_optimize:
# To read line-oriented protocol (like HTTP) from a socket (and
# avoid short read problem), it must be wrapped in a stream (aka
# file-like) object. That's how you do it in CPython:
client_stream = client_sock.makefile("rwb")
else:
# .. but MicroPython socket objects support stream interface
# directly, so calling .makefile() method is not required. If
# you develop application which will run only on MicroPython,
# especially on a resource-constrained embedded device, you
# may take this shortcut to save resources.
client_stream = client_sock
print("Request:")
req = client_stream.readline()
print(req)
while True:
h = client_stream.readline()
if h == b"" or h == b"\r\n":
break
print(h)
client_stream.write(CONTENT % counter)
client_stream.close()
if not micropython_optimize:
client_sock.close()
counter += 1
print()
main()
The requests are received properly and the GET variables are shown on the print(). The best i have been able to do is
req = client_stream.readline()
print(req)
while True:
h = client_stream.readline()
if h == b"" or h == b"\r\n":
break
print(h)
client_stream.write(CONTENT % counter)
//my function here:
if 'opengaragedoor=1' in req:
openGarageDoor()
client_stream.close()
I don't know how to parse the request properly. I only have come up with this dirty solution. This probably causes a timeout on the requesting system, as Postman or such needs to wait for the function to run through.

Kicking clients from server (Erlang)

I'm new to Erlang and I am writing a basic server. I am trying to figure out how to correctly kick a client from the server using the information that I have about the client (which is Pid, Client_socket, and Client_name.
Any suggestions would be great and much appreciated. Thanks for reading :)
Here's my code so far:
-module(cell_clnt).
-export([cell_client/0]).
cell_client()->
%%% Add any needed parameters for your cell process here
Port = 21,
Pending_connections = 5,
Cell = fun()-> cell_process() end,
spawn(fun()-> timer:sleep(10), keyboard_loop(Cell) end),
receive
stop->
ok
end.
keyboard_loop(Cell)->
case io:get_line(">> ") of
"quit\n"->
io:fwrite("Exiting...~n"),
if is_pid(Cell)-> Cell!stop; true->ok end;
"start\n" when is_function(Cell)->
keyboard_loop(spawn(Cell));
Input when is_pid(Cell)->
Cell!{input,Input},
keyboard_loop(Cell);
_Input->
io:fwrite("No cell process active yet!~n"),
keyboard_loop(Cell)
end.
%%% Edit this to implement your cell process %%%
cell_process()->
io:fwrite("In cell~n"),
{ok,Listening_socket} = gen_tcp:listen(21,
[binary,
{backlog,5},
{active,false},
{packet,line}]),
loop(Listening_socket,[]).
loop(Listening_socket, Clients)->
io:format("Clients: ~p", [Clients]),
case gen_tcp:accept(Listening_socket) of
{ok,Client_socket} ->
gen_tcp:send(Client_socket, "Hello, what is your name?"),
{_,Name} = gen_tcp:recv(Client_socket,0),
gen_tcp:send(Client_socket, "Hello, "),
gen_tcp:send(Client_socket, Name),
Pid = spawn(fun()-> client_loop(Client_socket) end),
loop(Listening_socket,[{Pid,Client_socket,Name}|Clients])
end.
client_loop(Client_socket)->
case gen_tcp:recv(Client_socket,0) of
{ok,Message}-> gen_tcp:send(Client_socket,Message),
client_loop(Client_socket);
{error,Why}-> io:fwrite("Error: ~s~n",[Why]),
gen_tcp:close(Client_socket)
end.
Use when you need close a TCP socket and kill process:
gen_tcp:close(Socket)
exit(Pid, kill).
You can close a socket by killing the pid, like this:
erlang:exit(Pid, kill)

wireshark count packets by port

I have a very large trace file and am trying to use Wireshark to determine which dest port has the most packets sent to it. Is there a way to get counts of packets sent to particular ports? Or to sort by number of packets sent a port?
You can write a simple wireshark listener in lua.
local tap
local ports = {}
local function packet(pinfo, tvb, userdata)
-- store number of packets per each port
local port = pinfo.dst_port
ports[port] = (ports[port] or 0) + 1
end
local function draw(userdata)
local maxi,maxv = 0,0
-- print all gathered statictics and find max
for i,v in pairs(ports) do
print(i .. ":", v)
if maxv < v then
maxi,maxv = i,v
end
end
print ("Max:", maxi, maxv)
end
local function reset(userdata)
ports = {}
end
local function show_ports()
tap = Listener.new()
tap.packet = packet
tap.draw = draw
tap.reset = reset
end
register_stat_cmd_arg('ports', show_ports)
Try it:
tshark -X lua_script:ports.lua -z ports -r in.pcap

Resources