How to handle multiple publishers on same port in zmq? - python

This question has been asked before, here. I have the exact same problem. I want to publish from a bunch of different processes, and use the same port every time.
I tried the solution presented in the answer, but this did not work for me. I get the error
File "/usr/local/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
self.run()
File "/usr/local/lib/python2.7/multiprocessing/process.py", line 114, in run
self._target(*self._args, **self._kwargs)
File "/home/akay/afk/multi.py", line 18, in to_zmq
socket.connect("tcp://*:%s" % port)
File "zmq/backend/cython/socket.pyx", line 478, in zmq.backend.cython.socket.Socket.connect (zmq/backend/cython/socket.c:4308)
ZMQError: Invalid argument
My code is like this, essentially taken straight from the example in the zmq docs here and here:
# Socket to talk to server
port = '5556'
context = zmq.Context()
socket = context.socket(zmq.SUB)
print "Listening for stream...", m
socket.bind("tcp://localhost:%s" % port) #change connect to bind, as per answer above
socket.setsockopt(zmq.SUBSCRIBE, topicfilter)
I am using python 2.7, and the most recent version of zmq. Any idea what I might be doing wrong?

Well, the error is clear:
[...]
socket.connect("tcp://*:%s" % port)
[...]
ZMQError: Invalid argument
You can't connect to *, you must specify an IP address (the server IP address). If both the client and the server run on a single machine, try with localhost or 127.0.0.1.

Related

Python Tornado WebSocket: not working on second network interface

I have WebSocket server and client app running on the same physical server. The server has two network interfaces and two IP addresses. When I run WebSocket server/client on first IP it works perfect. Bit it dosn't work on second.
Server:
port = 8080
ip1 = '192.168.100.11'
ip2 = '10.110.50.11'
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(port, ip1)
With second IP I got the following errors on client:
ERROR:tornado.application:Exception in callback <bound method WebSocketClient.keep_alive of <__main__.WebSocketClient instance at 0x7efc93d912d8>>
Traceback (most recent call last):
File "/opt/python/lib/python2.7/site-packages/tornado/ioloop.py", line 1229, in _run
return self.callback()
File "./client_rt_websocket.py", line 70, in keep_alive
self.ws.write_message("sendHeartbeat 1")
File "/opt/python/lib/python2.7/site-packages/tornado/websocket.py", line 1214, in write_message
return self.protocol.write_message(message, binary=binary)
File "/opt/python/lib/python2.7/site-packages/tornado/websocket.py", line 870, in write_message
raise WebSocketClosedError()
It seems like socket is closed and client cannot write message.
I've resolved the issue by restarting problematic network interface:
ifconfig eno2 down
ifconfig eno2 up

Using scapy with wifi

i've tried to send packet using scapy while connected to wifi, and i got an error about "dnet.pyx". But when i connect to wired connection, it worked.
I've searched a lot on the internet about this problem, but there wasn't any answer for this.
Is there a problem with scapy and wifi? Or is there something wrong with my computer/scapy?
edit:
send(IP(dst="204.11.192.171")/UDP(dport=5070)/"hello world")
with wifi, the error message was:
Traceback (most recent call last):
File "C:/Users/Tamir/PycharmProjects/SIP/main.py", line 10, in <module>
send(IP(dst="204.11.192.171")/UDP(dport=5070)/"hello world")
File "C:\Python27\lib\site-packages\scapy_real-2.2.0_dev-py2.7.egg\scapy\sendrecv.py", line 251, in send
__gen_send(conf.L3socket(*args, **kargs), x, inter=inter, loop=loop, count=count,verbose=verbose, realtime=realtime)
File "C:\Python27\lib\site-packages\scapy_real-2.2.0_dev-py2.7.egg\scapy\sendrecv.py", line 234, in __gen_send
s.send(p)
File "C:\Python27\lib\site-packages\scapy_real-2.2.0_dev-py2.7.egg\scapy\arch\pcapdnet.py", line 237, in send
ifs = dnet.eth(iff)
File "dnet.pyx", line 112, in dnet.eth.__init__ (./dnet.c:1764)
OSError: No such file or directory
with wired connection there was no error
Scapy does work on WiFi.
Notice that if you use a computer that has a wired connection as default (most non-laptops are like this), you should state what interface you wish to send the packet on. usually,
"eth0"
is the wired one, and
"wlan0"
or something like this is the WiFi.
When sending, add the field interface that way:
send(packet, iface="wlan0")
That also works when sniffing packets
sniff(iface=“wlan0”)
sniff(iface=“wlan0”, monitor=True) # not supported on every platform, with monitor mode on
I hope it helps.
There is probably just a problem connecting to the WiFi, the wired connection is a guaranteed connect, I have encountered the same occurrence and it usually because the WiFi isn't the stronger connection

Socket error in python server code

I have a python server script given below
# create the server, binding to localhost on port 9999
#
server = SocketServer.TCPServer((options.ip, options.port), cmdHandler)
While running a Python server script I am getting the following error:
File "C:\Devcon\OCDServer_New.py", line 225, in runTest
server = SocketServer.TCPServer((options.ip, options.port), cmdHandler)
File "C:\Python27\lib\SocketServer.py", line 419, in __init__
self.server_bind()
File "C:\Python27\lib\SocketServer.py", line 430, in server_bind
self.socket.bind(self.server_address)
File "C:\Python27\lib\socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
**socket.error: [Errno 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted**
How to solve this? In one link I found socket shutdown is a solution. But here the issue is coming while creating the socket itself. It might be because it was opened previously and not closed correctly. But I am unable to reset the entire thing and run the script again. Got stuck here.
Try using a different port to see if you get the same message or a different one. Something else could be using the same port therefore your code not being able to use it.

Connecting to Gmail from Python

I've been trying to connect to my Gmail account using python. imap is enabled.
import imaplib
imap_server = imaplib.IMAP4_SSL("imap.gmail.com",993)
# also tried imap_server = imaplib.IMAP4_SSL("imap.gmail.com"), doesnt work.
Traceback is :
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
imap_server = imaplib.IMAP4_SSL("imap.gmail.com",993)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 1202, in __init__
IMAP4.__init__(self, host, port)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 172, in __init__
self.open(host, port)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 1217, in open
IMAP4.open(self, host, port)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 248, in open
self.sock = self._create_socket()
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 1205, in _create_socket
sock = IMAP4._create_socket(self)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 238, in _create_socket
return socket.create_connection((self.host, self.port))
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/socket.py", line 435, in create_connection
raise err
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/socket.py", line 426, in create_connection
sock.connect(sa)
OSError: [Errno 65] No route to host
What OSError: [Errno 65] No route to host means is what it say: you can't get to that machine from your machine.
You can test that from outside of Python by opening up a terminal/DOS prompt and typing this:
ping imap.gmail.com
It's possible that this is actually a name lookup error, and you're somehow getting a bad address for imap.gmail.com. So, just to be sure, check by IP address too:
ping 74.125.129.108
ping 74.125.129.109
If ping works, you can check whether your router is for some reason just blocking TCP access to the host, e.g., with:
telnet imap.gmail.com
If it's working, this should either hang for a long time, or give you a connection-refused error; if it gives you a no-route-to-host error, it's the same problem you're seeing.
It's also possible that your router is specifically blocking port 993. You can test this too:
telnet imap.gmail.com 993
If it doesn't come back with something like "Connected to gmail-imap.l.google.com", same problem here too.
At any rate, once you've verified that this is a system or network configuration problem, not a programming problem, go ask for help with your system on the appropriate site.

How do I change permissions to a socket?

I am trying to run a simple Python based web server given here.
And I get the following error message:
Traceback (most recent call last):
File "webserver.py", line 63, in <module>
main()
File "webserver.py", line 55, in main
server = HTTPServer(('', 80), MyHandler)
File "/usr/lib/python2.5/SocketServer.py", line 330, in __init__
self.server_bind()
File "/usr/lib/python2.5/BaseHTTPServer.py", line 101, in server_bind
SocketServer.TCPServer.server_bind(self)
File "/usr/lib/python2.5/SocketServer.py", line 341, in server_bind
self.socket.bind(self.server_address)
File "<string>", line 1, in bind
socket.error: (13, 'Permission denied')
As far as I understand my firewall blocks access to a socket? Am I right? If it is the case, how can I change the permissions? Is it dangerous to change these permissions?
If you want to bind to port numbers < 1024, you need to be root. It's not a firewall
issue; it's enforced by the operating system. Here's a reference from w3.org,
and a FAQ entry specific to Unix.
If you want to run on a port under 1024, you'll need to be root. You can open the socket and drop root's permission for the rest of your program by switching to another user.
Most times it's easier to run a real webserver (say nginx) on port 80 and proxy the requests through to your program which you can run on a high numbered port (8080 for example). This way you don't need to worry about screwing something up during the time your process is running as root, as it never runs as root.
If it's just for testing, run the server on port 8080 and connect at http://localhost:8080/

Categories