Python permission error creating socket in Ubuntu - python

I am using socketCAN to interface to a CAN bus in Ubuntu 16.04. The interface works fine on my machine. However, when I kick off our CI, it executes on a remote agent (also Ubuntu 16.04) and I am receiving a permission error when the socket is creation is attempted:
Traceback (most recent call last):
File "main.py", line 20, in <module>
from tests_smoke import SmokeTests
File ".../tests_smoke.py", line 9, in <module>
from tests.validate_can import ValidateCAN
File ".../tests/validate_can.py", line 16, in <module>
bus = can.interface.Bus(bustype='socketcan', channel='can0', bitrate=250000)
File "/usr/local/lib/python3.5/dist-packages/can/interface.py", line 128, in __new__
return cls(channel, *args, **config)
File "/usr/local/lib/python3.5/dist-packages/can/interfaces/socketcan/socketcan.py", line 459, in __init__
self.socket = create_socket()
File "/usr/local/lib/python3.5/dist-packages/can/interfaces/socketcan/socketcan.py", line 330, in create_socket
sock = socket.socket(PF_CAN, socket.SOCK_RAW, CAN_RAW)
File "/usr/lib/python3.5/socket.py", line 134, in __init__
_socket.socket.__init__(self, family, type, proto, fileno)
PermissionError: [Errno 1] Operation not permitted
Being a novice in both Linux and Python, I'm not positive how to address this. I read in a few places that this may be a file creation permission issue which is inherited from the calling process so I have placed a "os.umask(0)" at the beginning of the script and that had no impact.
I've also read that it may be necessary to run the script as root. This seems quite risky.
What is the best way to fix this?

Related

Can't ping localhost with python [duplicate]

This question already has an answer here:
ICMP pinger application in Python - error: operation not permitted?
(1 answer)
Closed 2 months ago.
By running this simple code in Python
>>> from pythonping import ping
>>> ping('127.0.0.1', verbose=True)
I get this error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/j0tz/.local/lib/python3.10/site-packages/pythonping/__init__.py", line 81, in ping
comm = executor.Communicator(target, provider, timeout, interval, socket_options=options, verbose=verbose, output=out,
File "/home/j0tz/.local/lib/python3.10/site-packages/pythonping/executor.py", line 293, in __init__
self.socket = network.Socket(target, 'icmp', options=socket_options, source=source)
File "/home/j0tz/.local/lib/python3.10/site-packages/pythonping/network.py", line 31, in __init__
self.socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, self.protocol)
File "/usr/lib/python3.10/socket.py", line 232, in __init__
_socket.socket.__init__(self, family, type, proto, fileno)
PermissionError: [Errno 1] Operation not permitted
I tried running it as sudo but to no avail.
I don't know what the cause of the error is, so I'm hoping for intelligent answers from knowledgeable people.
Pythonping is a public repository I found on PyPI.
In the code I try to simply ping Localhost, returning the results on the screen when I set the "verbose" parameter to True.
you need root user permission to use it
run python3 such as root user then everything will be OK. and if you use it on a file run it with the root user.

Scope Tektronix TDS 1012 gives an error with pyvisa

I'm trying to connect a Tektronix TDS 1012B oscilloscope to my PC using Pyvisa.
I'm using Kubuntu 20.04, Python version 3.9.7 and Pyvisa version 1.11.3.
I do the following :
>>> import pyvisa
>>> rm = pyvisa.ResourceManager()
>>> rm.list_resources()
('USB0::1689::870::C021027::0::INSTR')
>>> scope = rm.open_resource('USB0::1689::870::C021027::0::INSTR')
>>> scope.query("*IDN?")
But I get this error message:
Traceback (most recent call last):
File "/home/pierre/anaconda3/lib/python3.9/site-packages/pyvisa_py/protocols/usbtmc.py", line 256, in write
return self.usb_send_ep.write(data)
File "/home/pierre/anaconda3/lib/python3.9/site-packages/usb/core.py", line 408, in write
return self.device.write(self, data, timeout)
File "/home/pierre/anaconda3/lib/python3.9/site-packages/usb/core.py", line 989, in write
return fn(
File "/home/pierre/anaconda3/lib/python3.9/site-packages/usb/backend/libusb1.py", line 837, in bulk_write
return self.__write(self.lib.libusb_bulk_transfer,
File "/home/pierre/anaconda3/lib/python3.9/site-packages/usb/backend/libusb1.py", line 938, in __write
_check(retval)
File "/home/pierre/anaconda3/lib/python3.9/site-packages/usb/backend/libusb1.py", line 602, in _check
raise USBTimeoutError(_strerror(ret), ret, _libusb_errno[ret])
usb.core.USBTimeoutError: [Errno 110] Operation timed out
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/pierre/anaconda3/lib/python3.9/site-packages/pyvisa/resources/messagebased.py", line 638, in query
self.write(message)
File "/home/pierre/anaconda3/lib/python3.9/site-packages/pyvisa/resources/messagebased.py", line 197, in write
count = self.write_raw(message.encode(enco))
File "/home/pierre/anaconda3/lib/python3.9/site-packages/pyvisa/resources/messagebased.py", line 157, in write_raw
return self.visalib.write(self.session, message)[0]
File "/home/pierre/anaconda3/lib/python3.9/site-packages/pyvisa_py/highlevel.py", line 543, in write
written, status_code = self.sessions[session].write(data)
File "/home/pierre/anaconda3/lib/python3.9/site-packages/pyvisa_py/usb.py", line 179, in write
count = self.interface.write(data)
File "/home/pierre/anaconda3/lib/python3.9/site-packages/pyvisa_py/protocols/usbtmc.py", line 436, in write
bytes_sent += raw_write(data)
File "/home/pierre/anaconda3/lib/python3.9/site-packages/pyvisa_py/protocols/usbtmc.py", line 258, in write
raise ValueError(str(e))
ValueError: [Errno 110] Operation timed out
When I use another type of oscilloscope this very simple code works fine. And when I use Pyvisa under Windows instead of Kubuntu, it also works.
Can someone help me fix this annoying issue?
Check out this link to a post from Tek in Tek's forums. They suggest using the pyvisa-py native Python VISA backend. This command instructs to use the pyvisa-py backend
rm = visa.ResourceManager('#py')
and you install by
pip install pyvisa-py
I don't have an Ubuntu machine at my fingertips so I don't know if there are packages available for installing from apt.
Connecting my TBS 1064 in SUSE has problems due to write permissions. This results in an time-out on requests like ask(). It is likely due to udev/rules, although I did not figure out what to put exactly to make it always work. For now one has to put the write permission (admin) for every USB reconnect, but then it is working.
Other devices do not have this problem and it is only a linux issue.

Is there a way to use Torsocks directly on a Python script that runs Selenium?

I have a simple Python script that runs Selenium, and I have tried using Torsocks (as usual) simply like this: torsocks python script.py. However, it failed with this error:
Traceback (most recent call last):
File "script.py", line 21, in <module>
browser = webdriver.Firefox(options=options)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/firefox/webdriver.py", line 163, in __init__
log_path=service_log_path)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/firefox/service.py", line 47, in __init__
self, executable_path, port=port, log_file=log_file, env=env)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/common/service.py", line 42, in __init__
self.port = utils.free_port()
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/common/utils.py", line 37, in free_port
free_socket.listen(5)
PermissionError: [Errno 1] Operation not permitted
Is it actually possible to use Torsocks like this?
I realize that I could send the request with SOCKS5 proxy, but I wonder if it could run using Torsocks, and if not, it would be great to get an explanation.

Trying to connect to coap resource with python library

So im trying to connect to a CoaP Resource using this python library https://github.com/chrysn/aiocoap . The library uses python 3.4 and i have 3.4 installed and set as the interpreter to use with this (Im on Windows 7 btw). Still im getting this error message, when executing the clientGET.py file. Same for the server file.
C:\Python34\python.exe C:/Learning/PyCoap/aiocoap/clientGET.py
Traceback (most recent call last):
File "C:/Learning/PyCoap/aiocoap/clientGET.py", line 34, in <module>
asyncio.get_event_loop().run_until_complete(main())
File "C:\Python34\lib\asyncio\base_events.py", line 268, in run_until_complete
return future.result()
File "C:\Python34\lib\asyncio\futures.py", line 277, in result
raise self._exception
File "C:\Python34\lib\asyncio\tasks.py", line 236, in _step
result = next(coro)
File "C:/Learning/PyCoap/aiocoap/clientGET.py", line 20, in main
protocol = yield from Context.create_client_context()
File "C:\Learning\PyCoap\aiocoap\aiocoap\protocol.py", line 510, in create_client_context
transport, protocol = yield from loop.create_datagram_endpoint(protofact, family=socket.AF_INET6)
File "C:\Python34\lib\asyncio\base_events.py", line 675, in create_datagram_endpoint
waiter)
File "C:\Python34\lib\asyncio\selector_events.py", line 68, in _make_datagram_transport
address, waiter, extra)
File "C:\Python34\lib\asyncio\selector_events.py", line 911, in __init__
super().__init__(loop, sock, protocol, extra)
File "C:\Python34\lib\asyncio\selector_events.py", line 452, in __init__
self._extra['sockname'] = sock.getsockname()
OSError: [WinError 10022] Ein ungultiges Argument wurde angegeben
Process finished with exit code 1
I didn't explore this in a real Python, as I don't have a Windows machine with Python 3.4 handy, but it seems to me that this could be a bug in asyncio. Its UDP socket creation probably simply doesn't work on Windows. Do some experimenting on the lower level, looking at what aiocoap is doing, and try to prove me wrong.
It's supposed to work, documentation only mentions ProactorEventLoop as not supporting UDP.
The error condition is described in Socket.error: Invalid Argument supplied .
aiocoap.protocol.Context.create_client_context() seems to be doing the right thing according to asyncio documentation, but _SelectorTransport.__init__() will always call sock.getsockname() before any packets are sent, at which point the socket will not be bound to an address (according to the linked SO question) and getsockname() will fail on Windows.
You might want to retry with a current version of Python and aiocoap (current development version, after 0.4a1). Windows used not to be supported in aiocoap, and is still not supporting all of CoAP, but now uses a socket implementation that is aware of some limitations in the Windows socket API.

SUDS Exception Imported Schema Failed

I'm getting the error:
Exception: imported schema (http://www.w3.org/2001/XMLSchema) at
(http://www.w3.org/2001/XMLSchema.x sd), failed
when passing a Doctor (constructed with ImportDoctor) to the suds Client constructor.
I'm working on two Windows machines, both of them got the same version of suds installed, but only one of them rises the error above.
Could someone guide me here to know why this error rises?, so I can figure out what's missing on the machine where it happens?.
Thanks in advance!!!.
UPDATE: I don't really know if this is important, but it's worth noting that my Windows machine that is rising the error is an Amazon Web Services instance. At my local machine everything's working well!.
UPDATE: Here's some code I ran at the python interpreter of the machine I mentioned. Here you can detail how the error is rising...
>>> from suds.client import Client
>>> from suds.xsd.doctor import ImportDoctor, Import
>>> missing_import = Import("http://www.w3.org/2001/XMLSchema")
>>> missing_import.filter.add("http://tempuri.org/")
>>> doctor = ImportDoctor(missing_import)
>>> client = Client("http://etcfulfill.ebooks.com/Fulfillment.asmx?wsdl")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "suds\client.py", line 112, in __init__
self.wsdl = reader.open(url)
File "suds\reader.py", line 152, in open
d = self.fn(url, self.options)
File "suds\wsdl.py", line 159, in __init__
self.build_schema()
File "suds\wsdl.py", line 220, in build_schema
self.schema = container.load(self.options)
File "suds\xsd\schema.py", line 95, in load
child.dereference()
File "suds\xsd\schema.py", line 323, in dereference
midx, deps = x.dependencies()
File "suds\xsd\sxbasic.py", line 422, in dependencies
raise TypeNotFound(self.ref)
suds.TypeNotFound: Type not found: '(schema, http://www.w3.org/2001/XMLSchema, )'
>>> client = Client("http://etcfulfill.ebooks.com/Fulfillment.asmx?wsdl", doctor=doctor)
No handlers could be found for logger "suds.xsd.sxbasic"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "suds\client.py", line 112, in __init__
self.wsdl = reader.open(url)
File "suds\reader.py", line 152, in open
d = self.fn(url, self.options)
File "suds\wsdl.py", line 159, in __init__
self.build_schema()
File "suds\wsdl.py", line 220, in build_schema
self.schema = container.load(self.options)
File "suds\xsd\schema.py", line 93, in load
child.open_imports(options)
File "suds\xsd\schema.py", line 305, in open_imports
imported = imp.open(options)
File "suds\xsd\sxbasic.py", line 542, in open
result = self.download(options)
File "suds\xsd\sxbasic.py", line 567, in download
raise Exception(msg)
Exception: imported schema (http://www.w3.org/2001/XMLSchema) at (http://www.w3.org/2001/XMLSchema.xsd), failed
UPDATE:
I realized that suds connections always open in TCP increasing ports, and if it reaches the maximum TCP port (65535) then it starts opening again from the minimum TCP port available, so there's no problem with this.
The problem shows up when using suds ImportDoctor, because it has to open a previous connection to the location where the import should be retrieved, and for some reason, if the system reaches the maximum TCP port count, then suds somehow assumes that there's no TCP port available to open the connection for obtaining the import, and in consecuence it throws the exception:
Exception: imported schema (http://www.w3.org/2001/XMLSchema) at (http://www.w3.org/2001/XMLSchema.xsd), failed
I repeat, this only happens if suds has to open this previous connection for obtaining the import. If ImportDoctor is not used, then suds has no problem if the TCP port count reaches its maximum, it just restarts at the minimum port available.
Does anyone has any clue on how to resolve this issue???. I'd really appreciate the help!!!.
I've figured out what the problem was. The schema that was missing from the WSDL I was trying to use with suds was:
http://www.w3.org/2001/XMLSchema
And the XSD file for this schema is at:
http://www.w3.org/2001/XMLSchema.xsd
So when I used suds ImportDoctor to add this schema import, sometimes the w3.org domain was denying my access (don't know why really) and that's why this error was rising:
Exception: imported schema (http://www.w3.org/2001/XMLSchema) at (http://www.w3.org/2001/XMLSchema.xsd), failed
What did I do to solve this problem?. I just downloaded this schema to my machine and used suds ImportDoctor to retrieve this import locally.
And that was it!!!. Confusing bug!!!. But SOLVED.

Categories