I am using python 2.7.2 on a windows 7 machine.
my code:
from multiprocessing import Process
def dummy_ftp_server(local_interface, username, password, homedir, perms):
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer
authorizer = DummyAuthorizer()
authorizer.add_user(username, password, homedir, perm=perms)
handler = FTPHandler
handler.authorizer = authorizer
server = FTPServer((local_interface, 21), handler)
server.serve_forever()
process = Process(target=dummy_ftp_server, args=('127.0.0.1', 'user', 'pass', "C:/path/test", 'elradfmw'))
process.start()
when trying to run the ftp server using multiprocessing.process i get this error
Traceback (most recent call last):
File "C:\path\test_ftp.py", line 18, in dummy_ftp_server
server = FTPServer((local_interface, 21), handler)
File "C:\Python27\lib\site-packages\pyftpdlib\servers.py", line 145, in init
self._af = self.bind_af_unspecified(address_or_socket)
File "C:\Python27\lib\site-packages\pyftpdlib\ioloop.py", line 733, in bind_af_unspecified
raise socket.error(err)
error: [Errno 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted
if i run it without using multiprocessing.process by using
dummy_ftp_server('127.0.0.1', 'user', 'pass', "C:/path/test", 'elradfmw')
it works fine.
on a linux machine i dont have this problem.
You are trying to run multiple ftp servers on the same ip address with the same port.
That is what the error is telling you.
It could be you already have something running on 127.0.0.1:21
check in a console with netstat -an
Related
I am writing a webscraper using Python and Beutifulsoup.
It was not long before my IP got blocked. I now need to rotate my IP so that I can connect to the website and scrape the required data.
I mostly followed tutorials and git repo documentation:
https://medium.com/#abhishek_menon/anonymous-web-crawling-in-10-minutes-edff782decdc
https://github.com/baatout/tor-ip-rotation-python-example
I am just following the tutorials line by line, not 100% certain if I am doing the right things.
I have set the torrc file to:
# This file was generated by Tor; if you edit it, comments will not be preserved
# The old torrc file was renamed to torrc.orig.1, and Tor will ignore it
ClientOnionAuthDir /Users/user/Library/Application Support/TorBrowser-Data/Tor/onion-auth
DataDirectory /Users/user/Library/Application Support/TorBrowser-Data/Tor
GeoIPFile /Applications/Tor Browser.app/Contents/Resources/TorBrowser/Tor/geoip
GeoIPv6File /Applications/Tor Browser.app/Contents/Resources/TorBrowser/Tor/geoip6
ControlPort 9051
HashedControlPassword my_hashed_password
CookieAuthentication 1
The my_hashed_password I got by running tor --hash-password my_password.
I went on to create a config file in the directory where privoxy is installed with the following content:
forward-socks5 / 127.0.0.1:9050 .
Every time I change something in these two files I run a short script to restart the services and call privoxy to check everything is ok:
brew services restart tor
brew services restart privoxy
privoxy
When I run a test script:
import time
from urllib.request import ProxyHandler, build_opener, install_opener, Request, urlopen
from stem import Signal
from stem.control import Controller
class TorHandler:
def __init__(self):
self.headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.73.11 (KHTML, like Gecko) Version/7.0.1 Safari/537.73.11'}
def open_url(self, url):
# communicate with TOR via a local proxy (privoxy)
def _set_url_proxy():
proxy_support = ProxyHandler({'http': '127.0.0.1:8118'})
opener = build_opener(proxy_support)
install_opener(opener)
_set_url_proxy()
request = Request(url, None, self.headers)
return urlopen(request).read().decode('utf-8')
#staticmethod
def renew_connection():
__TOR_password__ = 'my_password'
__TOR_hashed_password__ = 'my_hashed_password'
with Controller.from_port(port=9051) as controller:
controller.authenticate(password=__TOR_password__)
controller.signal(Signal.NEWNYM)
controller.close()
if __name__ == '__main__':
wait_time = 2
number_of_ip_rotations = 3
tor_handler = TorHandler()
ip = tor_handler.open_url('http://icanhazip.com/')
print('My first IP: {}'.format(ip))
# Cycle through the specified number of IP addresses via TOR
for i in range(0, number_of_ip_rotations):
old_ip = ip
seconds = 0
tor_handler.renew_connection()
# Loop until the 'new' IP address is different than the 'old' IP address,
# It may take the TOR network some time to effect a different IP address
while ip == old_ip:
time.sleep(wait_time)
seconds += wait_time
print('{} seconds elapsed awaiting a different IP address.'.format(seconds))
ip = tor_handler.open_url('http://icanhazip.com/')
print('My new IP: {}'.format(ip))
Note: I have tried both TOR_password and TOR_hashed_password.
I receive the following output:
"/Users/code/venv/bin/python" "/Users/code/proxy_rotation.py"
My first IP: 185.220.101.16
Traceback (most recent call last):
File "/Users/code/venv/lib/python3.8/site-packages/stem/socket.py", line 535, in _make_socket
control_socket.connect((self.address, self.port))
ConnectionRefusedError: [Errno 61] Connection refused
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/code/proxy_rotation.py", line 48, in <module>
tor_handler.renew_connection()
File "/Users/code/proxy_rotation.py", line 29, in renew_connection
with Controller.from_port(port=9051) as controller:
File "/Users/code/venv/lib/python3.8/site-packages/stem/control.py", line 1033, in from_port
control_port = stem.socket.ControlPort(address, port)
File "/Users/code/venv/lib/python3.8/site-packages/stem/socket.py", line 503, in __init__
self.connect()
File "/Users/code/venv/lib/python3.8/site-packages/stem/socket.py", line 172, in connect
self._socket = self._make_socket()
File "/Users/code/venv/lib/python3.8/site-packages/stem/socket.py", line 538, in _make_socket
raise stem.SocketError(exc)
stem.SocketError: [Errno 61] Connection refused
Process finished with exit code 1
I would appreciate some assistance in:
Resolving this issue and use Tor to rotate my IP
Any suggestions on a better way to rotate IPs with Python on a local host. Ill place it in a docker container when I have this working
Thank you
It looks like my Tor was not started.
When I manually start it as an application I can connect and request a website.
Ill have to look at how I can automatically start Tor without having to start it from my applications
I am trying to connect to a remote server and list files inside a path using the below code:
import pysftp
myHostname = "myhostname.com"
myPort = <someportnumber>
myUsername = "<valid username>"
myPassword = "<valid password>"
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
with pysftp.Connection(host=myHostname, username=myUsername, password=myPassword, cnopts= cnopts, port=myPort) as sftp:
print("Connection succesfully established ... ")
sftp.chdir('/logs/dev')
# Obtain structure of the remote directory
directory_structure = sftp.listdir_attr()
# Print data
for attr in directory_structure:
print(attr.filename, attr)
But when I am running it, It is unable to establish a connection. It's throwing below exception:
Traceback (most recent call last):
File "c:/Users/611841191/Documents/SFTP File Download/SFTPFileDownload.py", line 11, in <module>
with pysftp.Connection(host=myHostname, username=myUsername, password=myPassword, cnopts= cnopts, port=myPort) as sftp:
File "C:\Users\611841191\AppData\Roaming\Python\Python38\site-packages\pysftp\__init__.py", line 140, in __init__
self._start_transport(host, port)
File "C:\Users\611841191\AppData\Roaming\Python\Python38\site-packages\pysftp\__init__.py", line 176, in _start_transport
self._transport = paramiko.Transport((host, port))
File "C:\Users\611841191\AppData\Roaming\Python\Python38\site-packages\paramiko\transport.py", line 415, in __init__
raise SSHException(
paramiko.ssh_exception.SSHException: Unable to connect to <myhostname.com>: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time or established connection failed because connected host has failed to respond
Can anyone help me out with why this exception is being thrown because I tried with other remote servers and the code seems to be working fine for them? The code is throwing exception for one particular remote server only.
If your code cannot connect somewhere using some protocol (SFTP in this case), the first thing to test is, whether you can connect using the same protocol using any existing GUI/commandline client from the same machine that runs your code.
If that does not work either, you do not have a programming question, but a simple network connectivity problem.
If you need a help resolving the problem, please go to Super User or Server Fault.
I am trying to upload/download file to local FTP server, but it gives me the error mentioned in title.
For server I am using pyftpdlib:
import os
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer
# instantiate a dummy authorizer
authorizer = DummyAuthorizer()
# instantiate anonymous user to current directory
authorizer.add_anonymous(os.getcwd())
# FTP handler class
handler = FTPHandler
handler.authorizer = authorizer
# setup server on localhost, port = 21
address = ('', 21)
server = FTPServer(address, handler)
# set a limit for connections
server.max_cons = 10
server.max_cons_per_ip = 3
# start ftp server
server.serve_forever()
Here is client code:
from ftplib import FTP
# connect to FTP server
client = FTP(host="127.0.0.1")
client.login()
# list the contents of directory
client.retrlines('LIST')
But the FTP constructor throws:
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it.
Initially I thought it was port issue (I did not specify port from client when connected), so changed server port to 21, which I believe is default port.
When running server code, I get firewall alert, but when I give it the permission it runs normally. How can I connect to server from the client side?
I'm not sure what '' as an address would do on the server side. Either it is a wrong value in the first place. Or it may resolve to a different IP address than the 127.0.0.1. You should use the same value both on server and client side.
I'd start with 127.0.0.1 on the server side.
address = ('127.0.0.1', 21)
I'm getting a weird error while trying to execute an RPC using thrift on python. I have found similar issues online, but none of them really apply to my situation.
Here is the error I'm getting
No handlers could be found for logger "thrift.transport.TSocket"
Traceback (most recent call last):
File "experiment.py", line 71, in <module>
transport.open()
File "/usr/local/lib/python2.7/dist-packages/thrift/transport/TTransport.py", line 152, in open
return self.__trans.open()
File "/usr/local/lib/python2.7/dist-packages/thrift/transport/TSocket.py", line 113, in open
raise TTransportException(TTransportException.NOT_OPEN, msg)
thrift.transport.TTransport.TTransportException: Could not connect to any of [('192.168.178.44', 9000)]
The following is, I believe, the code which produces it.
TECS_SERVER_IP = "192.168.178.44"
TECS_SERVER_PORT = 9000
transport = TSocket.TSocket(TECS_SERVER_IP, TECS_SERVER_PORT)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = TTSService.Client(protocol)
transport.open()
This happens whenever I try to communicate to another machine, so I tried with the ip "127.0.0.1" and it works. However, using the IP of the localhost "192.168.178.44" which should refer to the same computer also produces the error.
To me it seems like it cannot resolve IP addresses for some reason...
Any ideas on what's causing this and how to fix it?
I'm using Python 2.7.12, thrift 0.9.3 and Ubuntu 16.04, but I also got the error on Windows 10.
This is how my thrift service starts
handler = TTSHandler()
handler.__init__()
transport = TSocket.TServerSocket(host='localhost', port=9000)
processor = TTSService.Processor(handler)
tfactory = TTransport.TBufferedTransportFactory()
pfactory = TBinaryProtocol.TBinaryProtocolFactory()
server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)
server.serve()
your server should bind to that address before client could connect to:
TSocket.TServerSocket(host='192.168.178.44', port=9000)
or use host='0.0.0.0' which means bind on all IPv4 addresses on the machine.
I am trying to use the a meteor ddp client to use the data from a meteor app in my python script. IT is a script that uses the Tor proxy API called stem. This is how my tor communicator looks like which works if ran separately:
Tor communicator (taken from the tor tutorial page with minor alterations):
import socket
import socks
import stem.process
import requests
from stem.util import term
from requestData import requestData
SOCKS_PORT = 7000
# Set socks proxy and wrap the urllib module
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, '127.0.0.1', SOCKS_PORT)
socket.socket = socks.socksocket
# Perform DNS resolution through the socket
def getaddrinfo(*args):
return [(socket.AF_INET, socket.SOCK_STREAM, 6, '', (args[0], args[1]))]
socket.getaddrinfo = getaddrinfo
def query(itemId):
"""
Uses requests to fetch a site using SocksiPy for Tor over the SOCKS_PORT.
"""
try:
return requestData(itemId)
except:
return "Unable to get data"
# Start an instance of Tor configured to only exit through Russia. This prints
# Tor's bootstrap information as it starts. Note that this likely will not
# work if you have another Tor instance running.
def print_bootstrap_lines(line):
if "Bootstrapped " in line:
print(line)
print(term.format("Starting Tor:\n", term.Attr.BOLD))
tor_process = stem.process.launch_tor_with_config(
config = {
'SocksPort': str(SOCKS_PORT),
'ExitNodes': '{ru}',
},
init_msg_handler = print_bootstrap_lines,
)
tor_process.kill() # stops tor
The above script is being ran from this script:
import Communicator
from MeteorClient import MeteorClient
client = MeteorClient('ws://127.0.0.1:3000/websocket')
client.connect()
def subscription_callback(error):
if error:
print(error)
client.subscribe('accounts', callback=subscription_callback)
all_posts = client.find('accounts')
print(all_posts)
Communicator.query("190aqe41vbewh7367f2hf27521")
But it is then giving me this result:
[1mStarting Tor:
[0m
May 10 13:21:45.000 [notice] Bootstrapped 0%: Starting
May 10 13:21:45.000 [notice] Bootstrapped 80%: Connecting to the Tor network
May 10 13:21:46.000 [notice] Bootstrapped 85%: Finishing handshake with first hop
May 10 13:21:46.000 [notice] Bootstrapped 90%: Establishing a Tor circuit
May 10 13:21:47.000 [notice] Bootstrapped 100%: Done
Traceback (most recent call last):
File "C:\Python34\lib\site-packages\socks.py", line 663, in connect
_BaseSocket.connect(self, proxy_addr)
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\gatsu\My Documents\LiClipse Workspace\TorCommunicator\MeteorDDP.py", line 5, in <module>
client.connect()
File "C:\Python34\lib\site-packages\python_meteor-0.1.6-py3.4.egg\MeteorClient.py", line 55, in connect
File "C:\Python34\lib\site-packages\python_ddp-0.1.5-py3.4.egg\DDPClient.py", line 119, in connect
File "C:\Python34\lib\site-packages\ws4py-0.3.4-py3.4.egg\ws4py\client\__init__.py", line 209, in connect
File "C:\Python34\lib\site-packages\socks.py", line 674, in connect
raise ProxyConnectionError(msg, error)
socks.ProxyConnectionError: Error connecting to SOCKS5 proxy 127.0.0.1:7000: [WinError 10061] No connection could be made because the target machine actively refused it
I solved this by importing the Communicator after I had done my Meteor stuff, right before I call a method in the Communicator.
from MeteorClient import MeteorClient
client = MeteorClient('ws://127.0.0.1:3000/websocket')
client.connect()
def subscription_callback(error):
if error:
print(error)
client.subscribe('accounts', callback=subscription_callback)
all_posts = client.find('accounts')
print(all_posts)
import Communicator
Communicator.query("190aqe41vbewh7367f2hf27521")