python: socketio-client not working - python-2.7

I have written this python code that uses socketio client module.
from socketIO_client import SocketIO
print "connecting to server"
socketIO = SocketIO('localhost', 8888, transports=['websocket'])
print "Connected"
def sendSocketId():
socketIO.emit('authenticate_python', "Python is connected")
def socketDisconnect():
socketIO.disconnect()
def doSomething(data):
print "message from ui : : ", data
socketIO.emit("msg_from_python","Message from python : : Hi! " + data)
try:
print socketIO.connected
if socketIO.connected:
sendSocketId()
socketIO.on('msg_from_node', doSomething)
socketIO.wait()
except Exception as e:
print "Exception : : ", e
socketDisconnect()
But when I connect to the server and run this file, it raises the following error:
Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
connecting to server
Traceback (most recent call last):
File "C:\Users\student\app.py", line 4, in <module>
socketIO = SocketIO('localhost', 8888, transports=['websocket'])
File "C:\Python27\lib\site-packages\socketIO_client\__init__.py", line 353, in __init__
resource, hurry_interval_in_seconds, **kw)
File "C:\Python27\lib\site-packages\socketIO_client\__init__.py", line 54, in __init__
self._transport
File "C:\Python27\lib\site-packages\socketIO_client\__init__.py", line 62, in _transport
self._engineIO_session = self._get_engineIO_session()
File "C:\Python27\lib\site-packages\socketIO_client\__init__.py", line 76, in _get_engineIO_session
transport.recv_packet())
StopIteration
>>>
I have no clue about this error and also there no no proper documentations as to how should we use this socketio client module.

The issue was resolved by uninstalling socket.io 2.0.0 version and installing watever version of it matches with the socketIO-client version.

This module socketIO-client-nexus fixes issue with compatible socket version socket.io protocol 1.x
https://pypi.org/project/socketIO-client-nexus/0.7.6/
which is not backwards compatible,
if you want to communicate using socket.io protocol 0.9 please use soketIO-client 0.5.7.2
https://pypi.org/project/socketIO-client/0.5.7.2/
Don't forget to change from import socketIO-client to socketIO-client-nexus

Related

CERTIFICATE_VERIFY_FAILED in urllib2 but not requests, on Python 2.7.18

My longstanding Python 2.7.17 environment got corrupted, and so I installed Python 2.7.18 using the macOS 64-bit installer downloadable from python.org. Now I get errors when loading HTTPS sites using urllib2 but not requests.
Here is the environment, running macOS Mojave:
Python 2.7.18 (v2.7.18:8d21aa21f2, Apr 19 2020, 20:48:48)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Works fine with requests:
>>> import requests
>>> url='https://github.com'
>>> requests.get(url)
<Response [200]>
Fails with urllib2:
>>> import urllib2
>>> urllib2.urlopen(url)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 429, in open
response = self._open(req, data)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 447, in _open
'_open', req)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 407, in _call_chain
result = func(*args)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1241, in https_open
context=self._context)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1198, in do_open
raise URLError(err)
urllib2.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:727)>
This occurs in both the plain 2.7.18 I installed and in virtual environments created from it. The problem is bigger than just urllib2, as certain database modules fail, too, that do not use urllib2. Moving to Python3 is not currently an option.
I tried pip install --upgrade certifi but that did not help.
Per [1] monkey-patching fixes the problem, but that is not a viable solution because this is destined to be production code.
Per [1] explicitly setting a non-verifying SSL context sometimes works, but again, it is not viable, plus it fails in certain third-party database interface code.
The answer may be in PEPP [2] or PEPP 493 [3], but the sun will swallow the earth before I understand those.
[1] urllib and "SSL: CERTIFICATE_VERIFY_FAILED" Error
[2] https://www.python.org/dev/peps/pep-0476/
[3] https://www.python.org/dev/peps/pep-0493/

Cannot import socketio (python-socketio) in Python2.7

Getting this error when trying to import socketio in python 2:
root#udacity:~/Desktop/CarND-Capstone-master/ros# python
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import socketio
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/socketio/__init__.py", line 3, in <module>
from .client import Client
File "/usr/local/lib/python2.7/dist-packages/socketio/client.py", line 6, in <module>
import engineio
File "/usr/local/lib/python2.7/dist-packages/engineio/__init__.py", line 3, in <module>
from .client import Client
File "/usr/local/lib/python2.7/dist-packages/engineio/client.py", line 302
'Unexpected response from server') from None
^
SyntaxError: invalid syntax
Tried reinstalling/installing socketio and engineio, but can't seem to get it to work. I should mention that I'm running in an Ubuntu VM.
I found the answer, as shown above, it was an engineio fault. Installing it before socketio in my requirements file worked. Particularly I'm using python-socketio==4.4.0, and python-engineio==3.11.2
python-engineio==3.11.2
python-socketio==4.4.0
Reference:
https://github.com/udacity/CarND-Capstone/issues/342

Using Django in Docker unable to fetch rest api

I have built the webapp using Django and also configured for Rest api. I am able to fetch the data via curl and also by python script. Issue I'm running into, when using Docker container, when user fill in the data and submit, it goes thru and ran the python function, but it stop working at when fetching the data via api and thru errors
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.6/site-packages/requests/models.py", line 897, in json
return complexjson.loads(self.text, **kwargs)
File "/usr/lib64/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/usr/lib64/python3.6/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib64/python3.6/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
I'm able to curl the URL inside the container. I get the same error when shell into the container in python3.6
sh-4.2# python3.6
Python 3.6.8 (default, May 2 2019, 20:40:44)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import requests, json
>>> url = 'http://127.0.0.1:8001/db/data/'
>>> response = requests.get(url)
>>> parsedjson = json.loads(response.text)
ERROR SAME AS ABOVE . . .
parsedjson = response.json()
ERROR SAME AS ABOVE . . .
>>> print(response.status_code)
503
>>> exit()
sh-4.2# curl http://127.0.0.1:8001/db/data/
[{"center":{"name":"ABC"},"main":{"code":{"name":"ABC"},"subnet":"10.10.12.0",...."}]
sh-4.2#
any idea? how to fix it?
thank you for the help in advance..

trying to connect to Windows winRM using kerberos winrm

so I have tested this under several different variations. I have a lab with Windows 2008 R2 servers and RHEL6 and Python2.6 installed. I am able to use ansible as means to remote into the windows server and perform a ping (I have followed all the instructions on how to do this).
I get the problem of not being able to do this with Rhel 7 and python2.7, however I am not sure that the difference in python versions is whats stopping me.
I constantly get this hostname_override error...
[alebede#linuxbox]$ klist -a
Ticket cache: FILE:/tmp/krb5cc_37575
Default principal: Admin_alebede#mydomain.bla
Valid starting Expires Service principal
02/10/2017 11:30:32 02/10/2017 21:30:32 krbtgt/mydmain.bla#mydomain.bla
renew until 02/10/2017 21:30:32
Addresses: (none)
[alebede#linuxbox]$ python
Python 2.7.5 (default, Oct 11 2015, 17:47:16)
[GCC 4.8.3 20140911 (Red Hat 4.8.3-9)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import winrm
>>> s = winrm.Session('WINDOWSBOX', auth=('admin_alebede#mydomain.bla', 'mypassword'), transport='kerberos')
>>> r = s.run_cmd('ipconfig', ['/all'])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/site-packages/winrm/__init__.py", line 37, in run_cmd
shell_id = self.protocol.open_shell()
File "/usr/lib/python2.7/site-packages/winrm/protocol.py", line 132, in open_shell
res = self.send_message(xmltodict.unparse(req))
File "/usr/lib/python2.7/site-packages/winrm/protocol.py", line 207, in send_message
return self.transport.send_message(message)
File "/usr/lib/python2.7/site-packages/winrm/transport.py", line 173, in send_message
self.session = self.build_session()
File "/usr/lib/python2.7/site-packages/winrm/transport.py", line 140, in build_session
sanitize_mutual_error_response=False)
TypeError: __init__() got an unexpected keyword argument 'hostname_override'
With ansible it looks like this, the same hostname_override error:
mywinserver.mydomain.com> WINRM CONNECT: transport=ssl endpoint=https://mywinserver.mydomain.com:5986/wsman
<mywinserver.mydomain.com> WINRM CONNECTION ERROR: the specified credentials were rejected by the server
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/ansible/plugins/connection/winrm.py", line 154, in _winrm_connect
self.shell_id = protocol.open_shell(codepage=65001) # UTF-8
File "/usr/lib/python2.7/site-packages/winrm/protocol.py", line 132, in open_shell
res = self.send_message(xmltodict.unparse(req))
File "/usr/lib/python2.7/site-packages/winrm/protocol.py", line 207, in send_message
return self.transport.send_message(message)
File "/usr/lib/python2.7/site-packages/winrm/transport.py", line 190, in send_message
raise InvalidCredentialsError("the specified credentials were rejected by the server")
InvalidCredentialsError: the specified credentials were rejected by the server
mywinserver.mydomain.com | UNREACHABLE! => {
"changed": false,
"msg": "kerberos: __init__() got an unexpected keyword argument 'hostname_override', ssl: the specified credentials were rejected by the server",
"unreachable": true
}
Let me know what else I can try, also on the Windows 2008R2 server i can see that the linux box is trying to connect in the security event log. Not sure what else is going on. Again, this works in RHEL6 on 2 different windows 2008R2 servers.
Likely you have a mismatch in versions between winrm and requests-kerberos. I had the same issue when I had winrm installed via pip and requests-kerberos install via apt.
Verify that you are using a single package manager for all installations.

Python 2.7 & wget error 17 "No usable temporary file name found"

I am just learning python 2.7 and trying to use wget to download a file from a URL.
I get the following error (and here are the exact input & syntax):
Python 2.7.10 (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import wget
>>> wget.download('http://www.csun.edu/~rd436460/Labview/Lecture-Overview.pdf')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\wget.py", line 506, in download
(fd, tmpfile) = tempfile.mkstemp(".tmp", prefix=prefix, dir=".")
File "C:\Python27\lib\tempfile.py", line 314, in mkstemp
return _mkstemp_inner(dir, prefix, suffix, flags)
File "C:\Python27\lib\tempfile.py", line 257, in _mkstemp_inner
raise IOError, (_errno.EEXIST, "No usable temporary file name found")
IOError: [Errno 17] No usable temporary file name found
>>>
My question is....... What is the correct syntax to get this file to download without error?
I am running Python 2.7 On Windows 10...... Also I saw some of the other post of temporary file errors due to no drive space. On my system I have plenty of drive space available.