I want to work with Tor through Stem module.The following is my code to get connected with Tor:
import sys
from stem.connection import connect
if __name__ == '__main__':
controller = connect()
if not controller:
sys.exit(1) # unable to get a connection
print 'Tor is running version %s' % controller.get_version()
controller.close()
However, i get following error when running the code:Unable to connect to tor. Are you sure it's running?
I turned on Tor and tested the code again and nothing happened. The problem is this line:
if __name__ == '__main__':
controller = connect()
Even if I enter the connect() method to Python interpreter I get the error. What is the error and how can I fix it?
Related
hei guys,
I have an executable python script, say get_data.py (located in project_x/src/) which is working properly, when started by: python get_data.py . It gets data (a list of id's which are necessary for further calculations) from a database via mysql.connector and then processes these data in parallel (via multiprocessing) using pool.map.
BUT it is supposed to be started by an .exe-file (located in project_x/exec/)[EDIT: This .exe uses the php command exec() to directly addresses my python script]. This is not working properly but ending in the try-except-block (in wrapper_fun) when catching the (unknown) mistake and not terminating when deleting the try-except-commands.
Do you have any idea what could be going wrong? I would appreciate any idea. I tried logging but there seems to be a permission problem. My idea is that the connection the db cannot be established and therefore there are no id's.
def calculations():
do_something...
def wrapper_fun(id):
try:
calculations(id)
except Exception:
return(False)
if __name__ == "__main__":
import multiprocessing
import mysql.connector
from mysql.connector import Error
host_name = <secret_1>
user_name = <secret_2>
passt = <secret_3>
connection = None
try:
connection = mysql.connector.connect(
host=host_name,
user=user_name,
passwd=user_password
)
except Error as err:
print(f"Error: '{err}'")
d = pd.read_sql_query(query, connection,coerce_float=False)
connection.close()
id_s = list(d.ids)
results = [pool.map(wrapper_fun,id_s)]
...
I am making a python program to write variables to an opc DA server.
I have the connection and others, but when trying to write values for a variable, the program does not respond and a windows error message appears saying:
My code:
import OpenOPC
import sys
opc = OpenOPC.client()
servers = opc.servers()
idServer = int(2)
print('connecting to opc server:', servers[idServer])
opc.connect(servers[idServer])
print('connection okey:', servers[idServer])
write = opc.write(('variableName', 1))
print('write:', write)
input('> ')
Does anyone know why the program crashes when it reaches that part? Thanks a lot
my goal is to get the updates of an rtd server in python
I've following call in excel which is working:
=RTD("xrtd.xrtd";;"EUCA")
For python I've found following client library: https://github.com/brotchie/pyrtd/blob/master/rtd/client.py
I tried to get a simple example where I can connect to the server
import sys
sys.path.append(".")
from client import RTDClient
name = "xrtd.xrtd"
try:
client = RTDClient(name)
client.connect(False)
client.register_topic('EUCA')
except Exception as identifier:
print(str(name) + " error : " + str(identifier))
My first problem was that I've used 64bit python, but after I solved this I receive following exception from the connect():
xrtd.xrtd error : This COM object can not automate the makepy process
please run makepy manually for this object
I've no idea what I've to do now. I've python experience but no experience with COM Objects
Try this
import pythoncom
from rtd import RTDClient
if __name__ == '__main__':
time = RTDClient('xrtd.xrtd')
time.connect()
time.register_topic('EUCA')
while 1:
pythoncom.PumpWaitingMessages()
if time.update():
print time.get('EUCA')
I am working on a tcp client-server python socket program where I have written server code to sent a simple message to the client . However when I run the server side in python idle I get invlalid syntax error and a red mark on the python version . I don't know where the problem is and I would appreciate your help with this specific task .
Image where error happens :
I press run and then run module and I get :
My code :
Server :
import sys
from socket import *
serverSocket = socket(AF_INET,SOCK_STREAM)
serverSocket.bind(('localhost',1234))
serverSocket.listen()
data = "Network labs"
while 1 :
connectionSocket ,addr = serverSocket.accept()
connectionSocket.send(data)
connectionSocket.close()
Client :
import sys
from socket import *
clientSocket = socket(AF_INET,SOCK_STREAM)
server_address=('localhost',1234)
clientSocket.connect(server_address)
sentence = clientSocket.recv(1024)
print(sentence)
clientSocket.close()
You tried to run the log of a shell session, complete with non-code startup message text and non-code prompts as python code. But the session log is not python code. "Python" might be, but "Python 3" is not valid code and so python reports a SyntaxError. This has nothing to do with running the code from an IDLE editor. If you run server.py from a command line or from any other python-aware editor or IDE, you would see the same.
To run server.py, you must remove the non-code parts -- the startup message and prompts. In general, you would also have to remove output, but there is none in your example. So you should end up with
import sys
from socked import *
...
In other words, the cleaned-up server code you listed in your question, which is not the code you ran in the screenshot to get the error message.
I have a python server to which I can do POST requests. This is the script
from bottle import Bottle, run, template, get, post, request
app = Bottle()
#app.route('/rotation', method='POST')
def set_rotation():
rotation = request.forms.get('rotation')
return rotation
run(app, host='localhost', port=8080)
So in the POST request I send the rotation value and get that in the script. I need the rotation value in another script so I do this in that script
from mybottle import set_rotation
print set_rotation
When I run the first script and then the second script, I get this error
socket.error: [Errno 98] Address already in use
I'm quite new to python so I don't have a clue as to what I'm doing wrong
If you want to be able to import without starting the run function use
if __name__=="__main__"
if __name__=="__main__":
run(app, host='localhost', port=8080)
Each time you import from the file run(app, host='localhost', port=8080) is going to be executed, using if __name__=="__main__" will only start the server when you execute the file itself so you will avoid your socket.error: [Errno 98] which you are getting trying to start the server when it is already running.
You should verify that no other program use the 8080 port, or simply change the port to another value.
I think you run the server twice. The error you get comes from the second server that can't bind on port 8080 because the first is already using it.
Your code, as given, will start a server when imported. This is probably not what you want.
You can avoid this behavior by test the name of your module, which is __main__ only if it's the called script:
if __name__ == '__main__':
run(app, host='localhost', port=8080)
Then, when imported, no server is ran.