I'm trying to use 'non-blocked socket' for a Python project
(see previous question if anyone has a better answer : How to use a socket without waiting in python )
I saw that people on the site suggested using the command:
socket.setblocking ()
But when I run the program it crashes, and the error is recorded:
AttributeError: module 'socket' has no attribute 'setblocking'
How can I fix this?
And is there another way?
It looks like you are trying to call setblocking() on the module called socket rather than on the object called socket.
Change your code to something like:
import socket
s = socket.socket(...)
s.setblocking(...)
Related
So I have been trying to use the ZMQ module to make a publisher subscriber model
I downloaded the library in Pycharm, made sure that it is the latest version.
And when I try to use things like ".setsockopt" it keeps saying "unresolved attribute reference setsockopt for class "Socket" and ask me to add a method to the class "Socket" This is exteremly annoying and I have no idea how to solve this. I went online to read tutorials and all they did is just "import zmq" and everything is fine. Please help me=(
Its not only setsockopt, I have been trying things like .send_pyobj and it doesn't work too!
Here I literally copy and paste a code from youtube: youtube.com/watch?v=-_CXA8SZsOs
please go to time 5:58, I copy that code and my pycharm say that on line15: socket.send_pyobj has error and say the same thing "unresolved attribute reference setsockopt for class "Socket"
Somebody please help me your help means a lot to me
The youtube code:
import zmq
from time import sleep
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind('tcp://127.0.0.1:2000')
messages = [100,200,300]
curMsg = 0
while(True):
sleep(1)
socket.send_pyobj({curMsg:messages[curMsg]})
if (curMsg == 2):
curMsg = 0
else:
curMsg = curMsg +1
I have the following traceback error incase you are wondering
note that it is for rec_obj which is pretty much the same issue with send_obj and also setsockopt. Basically it is missing for no reasons
File "C:\Users\jacky\PycharmProjects\testing_the_send_data_and_receive_data_thing\venv\lib\site-packa
ges\zmq\sugar\attrsettr.py", line 44, in getattr
raise AttributeError(
AttributeError: Socket has no such option: REC_PYOBJ
I'm trying to get the ODB library working. In the documentation at https://python-obd.readthedocs.io/en/latest/ it lists the following code:
import obd
connection = obd.OBD("/dev/ttyUSB0") # connects to USB or RF port
cmd = obd.commands.SPEED # select an OBD command (sensor)
response = connection.query(cmd) # send the command, and parse the response
print(response.value) # returns unit-bearing values thanks to Pint
print(response.value.to("mph")) # user-friendly unit conversions
When I put this in a file called test.py and I run it:
python2 test.py
I get the following error message:
Traceback (most recent call last):
File "test.py", line 1, in <module>
import obd
File "/home/ubuntu/obd.py", line 3, in <module>
AttributeError: 'module' object has no attribute 'OBD'
Stackoverflow comes up with several iterations of this error message, but none clearly explain the problem, only giving specific solutions to those libraries.
I guess it's obvious that I'm new to Python, and I'm having trouble interpreting this error message, even after several hours of writing several small Python programs. I am of course also interested in why the error message is so un-intuitive to a newcomer and where I can gain the common knowledge I might be missing, and I guess this is as good a case to discover that through as any.
So far I have figured out the following:
<module> refers to the name of my python script that I am trying to run.
The mistake I made at first, was to name my test.py file, initially obd.py. This conflicted with the import obd as it was trying to import the file itself - even after I deleted it! The reason was that when I tried to run it the first time, it created a file called obd.pyc - and even though obd.py was no longer there, import OBD found the initially created obd.pyc and tried to import that - which of course does not contain the OBD object from the library I was trying to use.
This is not the detailed answer I'm looking for, so please add a more detailed explanation - or a link to how Python compiling works, if you can.
Heading
I got the error ( attributeerror. 'MyTopo' object has no attribute 'addlink' ) when I tried running the "Fat-tree topology.py" script in mininet .Please help.
First, you have to be sure you imported the mininet library.
The fuction you need is called addLink, not addlink, which caused your error.
I am using Python Anaconda 2.7. I would like to toggle the port using serial communication, But I am getting error: AttributeError: 'module' object has no attribute 'Serial'
My sample program is:
import serial
import time
#remember to adjust “COM3”
port = serial.Serial("COM3", 19200, timeout=0.5)
#turn on port 2, sleep 2 seconds, turn off port 2
port.write(b"\nF2\r")
time.sleep(2.0)
port.write(b"\nF2\r")
#turn on port 2 and 7, sleep 2 seconds, turn off port 2 and 7
port.write(b"\nF2\r\nF7\r")
time.sleep(2)
port.write(b"\nF2\r\nF7\r")
#close the port
port.close()
I have tried many solutions:
changing file name from 'serial.py' to 'any_other_name.py' and vise versa
deleting related .pyc file
Installing 'pip install pyserial'
Doing From serial import serial
When I run the same program from psychopy, it is working really fine. I don't know How to solve it. If some one can give me suggestions that It would be great help for me. Thanking you in advance.
Ravi
Your code seems good, so the problem you encounter is probably due to a bad import.
You really should avoid to name your python script like "standard" modules (serial.py, string.py...), because by doing this, you expose yourself to accidentally import those files instead of the correct one (probably what happened to you).
If you need to be sure of what you're importing, try this :
import serial
print serial.__file__ # this will tell you the position of the file you've imported and help you to be sure of what you're using.
# in case you're not importing a module, only a class... try this :
help(serial) # even without any help, it will give you at the end the path where this object is defined :)
I am getting a syntax error in my code with Jython. Can anyone say what's wrong in the syntax? I am new to this language, don't have much of an idea.
Error Message :
WASX7017E: Exception received while running file "namespace.jy"; exception information: com.ibm.websphere.management.exception.InvalidAttributeValException: ADMG0012E: The attribute value for attribute integration/endpoint/account is not valid.
My Code:
import sys
nodeName =sys.argv[0]
serverName =sys.argv[1]
profilePath=sys.argv[2]
machineName=sys.argv[3]
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Main program
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
createNamespaceBinding(nodeName,serverName,profilePath,machineName)
I have added call to setJVMSystemProperties(), so it will ignore '/' in the name attribute, but am still facing the problem.
It worked for after adding this property file com.ibm.websphere.management.configservice.validateNames = false and then stopping and starting the server and then try to run jython script for namespace binding