Network is unreachable if running script on webhosting - python

The next script runs fine on my mac. When I try to run it on my WebHosting (bluehost) I'm getting socket.error: [Errno 101] Network is unreachable. Any idea how can I fix it?
#!/usr/bin/python
# Required header that tells the browser how to render the text.
print "Content-type: text/html\r\n\r\n";
import imaplib
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('user#gmail.com', 'password')
mail.list()
# Out: list of "folders" aka labels in gmail.
mail.select("inbox") # connect to inbox.
print mail.list()
Traceback (most recent call last):
File "test2.py", line 6, in <module>
mail = imaplib.IMAP4_SSL('imap.gmail.com')
File "/home4/user/python27/lib/python2.7/imaplib.py", line 1148, in __init__
IMAP4.__init__(self, host, port)
File "/home4/user/python27/lib/python2.7/imaplib.py", line 163, in __init__
self.open(host, port)
File "/home4/user/python27/lib/python2.7/imaplib.py", line 1159, in open
self.sock = socket.create_connection((host, port))
File "/home4/user/python27/lib/python2.7/socket.py", line 571, in create_connection
raise err
socket.error: [Errno 101] Network is unreachable
Their support isn't helpful at all.
Can it be port related or maybe SSL?

On bluehosts help pages they mention that outgoing connnections are restricted, so ther problem isn't with your program. The only way of getting outbound connections to be allowed seems to pay for it.

Related

Error to send e-mail using module smtplib

I'm doing a code to send emails automatically using Python and a local server (where I work). I don't know why this error happens.
I've tried to connect the server using commands from module smtplib -> smtplib.SMTP_SSL(hot, port) and smtplib.SMTP(hot,port) but both doesn't work.
import smtplib
server = smtplib.SMTP('IPfromCompanyServer')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\dbou\AppData\Local\Programs\Python\Python37\lib\smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "C:\Users\dbou\AppData\Local\Programs\Python\Python37\lib\smtplib.py", line 336, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Users\dbou\AppData\Local\Programs\Python\Python37\lib\smtplib.py", line 307, in _get_socket
self.source_address)
File "C:\Users\dbou\AppData\Local\Programs\Python\Python37\lib\socket.py", line 727, in create_connection
raise err
File "C:\Users\dbou\AppData\Local\Programs\Python\Python37\lib\socket.py", line 716, in create_connection
sock.connect(sa)
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
Make sure you're getting to Mail server IP and port number right. And some clients like Gmail don't allow you to send mails automatically without disabling the secure mail transfer feature.
Additionally you could add this piece of code to your program before you log in.
try:
self.smtp.ehlo()
self.smtp.starttls()
self.smtp.ehlo
except:
print "No TLS "
#login here
And maybe this link outta help out a little:
https://superuser.com/questions/1292420/sending-an-email-from-python-using-local-python-smtp-server

ConnectionRefusedError: [Errno 111] Connection refused

I was trying to use ftp and I am getting the following error:
>>> ftp = ftplib.FTP('192.168.248.108')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.5/ftplib.py", line 118, in __init__
self.connect(host)
File "/usr/lib/python3.5/ftplib.py", line 153, in connect
source_address=self.source_address)
File "/usr/lib/python3.5/socket.py", line 711, in create_connection
raise err
File "/usr/lib/python3.5/socket.py", line 702, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
I was trying to take it step by step since the whole client and server codes were not running. Help please. Thank you.
EDIT:
This is the client side code:
from ftplib import FTP
ftp = FTP('192.168.248.128')
ftp.login(user='username', passwd = 'password')
ftp.cwd('/Desktop/FTP')
def placeFile():
filename = 'myfile.txt'
ftp.storbinary('STOR '+filename, open(filename, 'rb'))
ftp.quit()
placeFile()
First of all check this ip to see if ftp service is available, and if it is check the port that it is listening on, cause it maybe (rare but possible) is configured to listen on a different port than the standard one - 21 . Also maybe the connection is blocked by a firewall, and that is why connection gets refused.
Also haven't seen the whole code of yours but I think another/different problem is this: def placeFile() should be changed to this instead def placeFile(ftp) - cause the function placeFile doesn't really know that ftp references to the ftp client you created above.

Using a Python script to establish FTP connection behind FTP proxy

Im trying to connect to an ftp server that is behind ftp proxy.
The proxy server does not require username and password, the ftp server does.
I have searched through several posts:
Proxies in python
How to use urllib2 to access ftp/http server using proxy with authentification
How to connect to ftp server via proxy using ftplib
How to specify an authenticated proxy for a python http connection?
Here is the last version of my code. So far I've figured out that to open the ftp server behind an ftp proxy, the notation ftp://username:password#server can be used. I'm using the urllib2 library to define the proxy server.
import urllib2
proxy_host = '101.11.44.84:8021' # only host name, no scheme (http/ftp)
proxy_handler = urllib2.ProxyHandler({'ftp': proxy_host})
auth = urllib2.FTPHandler()
try:
opener_thru_proxy = urllib2.build_opener(proxy_handler, auth)
except:
logger.exception('build_opener error')
raise
print opener_thru_proxy
try:
conn = opener_thru_proxy.open('ftp://user:password#100.159.66.113')
except:
logger.exception('opener thru proxy error')
raise
print conn.read()
conn.close()
The output of this code yields:
2016-05-23 16:15:28,286 - root - ERROR - opener thru proxy error
Traceback (most recent call last):
File "<string>", line 293, in <module>
File "C:\_Studna\Python\python-2.7.10.amd64\lib\urllib2.py", line 431, in open
File "C:\_Studna\Python\python-2.7.10.amd64\lib\urllib2.py", line 449, in _open
File "C:\_Studna\Python\python-2.7.10.amd64\lib\urllib2.py", line 409, in _call_chain
File "C:\_Studna\Python\python-2.7.10.amd64\lib\urllib2.py", line 1412, in ftp_open
File "C:\_Studna\Python\python-2.7.10.amd64\lib\urllib2.py", line 1434, in connect_ftp
File "C:\_Studna\Python\python-2.7.10.amd64\lib\urllib.py", line 875, in __init__
File "C:\_Studna\Python\python-2.7.10.amd64\lib\urllib.py", line 884, in init
File "C:\_Studna\Python\python-2.7.10.amd64\lib\ftplib.py", line 135, in connect
File "C:\_Studna\Python\python-2.7.10.amd64\lib\socket.py", line 575, in create_connection
URLError: <urlopen error ftp error: [Errno 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>
It seems that I'm unable to establish the ftp connection with the ftp server.
However, when I use the same set of connection data and use it in Total Commander, the connection works correctly. See the image enclosed Total Commander ftp connection window.

[PYTHON]Email and file connection error

I tried to find some topic related, found some, tried some suggested solution, but seems not to work.
I'm trying to figure out where I did it wrong, I'm basically listing a directory and putting the output into a text file that I want to send via smtp to dest address.
#!/usr/bin/python
import os, sys
import smtplib
from email.mime.text import MIMEText
# GRABE LISTED DIRECTORY AND CREATE THE FILE #
#open folder and list it
Winpath = "C:\"
dirs = os.listdir(Winpath)
#Put it into file
fo = open("activitygraber.txt", "w+")
for file in dirs:
fo.write(file + "\n")
# EMAIL IT TO GIVEN ADDRESS #
msg = MIMEText(fo.read())
fo.close()
msg["Subject"] = "Test ActivityGrab"
msg["From"] = "test#test.me"
msg["To"] = "dest#dest.com"
s = smtplib.SMTP("localhost")
s.sendmail("test#test.me", "dest#dest.com", msg.as_string())
s.quit()
That's simple code mostly from Python documentation (I'm in the learning curve).
The file is being created and ready to send I guess, but somehow the connection is not being created. I guess it's because my smtp port in not open ? should I tried to connect to a mail service ? then send it from there ?
Here the message I get when I try to send it:
Traceback (most recent call last):
File "C:\Users\PC\Desktop\testfile.py", line 33, in <module>
s = smtplib.SMTP("localhost")
File "C:\Python27\lib\smtplib.py", line 250, in __init__
(code, msg) = self.connect(host, port)
File "C:\Python27\lib\smtplib.py", line 310, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Python27\lib\smtplib.py", line 285, in _get_socket
return socket.create_connection((host, port), timeout)
File "C:\Python27\lib\socket.py", line 571, in create_connection
raise err
socket.error: [Errno 10061] No connection could be made because the target machine actively refused it
Any help of hint would really be appreciated thanks !
Meanwhile I'm gonna continue to search the cause.
Cheers,
s = smtplib.SMTP("localhost") isn't going to work unless you have a running SMTP server on your local machine (which you probably don't...).
If you do, make sure it's listening on port 25.
If you don't, you will have to use a remote SMTP server. You can use google's if you have a gmail account, or find a free one that is set up as a mail relay (that means you can use it anonymously), but these are very rare these days.

Python SSL FTP connection timing out

I'm trying to get a connection established to a FTP server with SSL from within Python (v3.3.0). But I keep getting a timeout. I am NOT using port 990 as the SSL port (paranoid). Would that be the cause of this problem? And if so, how do I specify the port I am using?
Here's my script:
from ftplib import FTP
from ftplib import FTP_TLS
ftps = FTP_TLS('ip address')
ftps.auth()
ftps.sendcmd('USER uname')
ftps.sendcmd('PASS password')
ftps.prot_p()
ftps.retrlines('LIST')
ftps.close()
And here is the result:
Traceback (most recent call last):
File "Scrpit name removed for posting", line 12, in <module>
ftps.retrlines('LIST')
File "C:\Python33\lib\ftplib.py", line 767, in retrlines
conn = self.transfercmd(cmd)
File "C:\Python33\lib\ftplib.py", line 381, in transfercmd
return self.ntransfercmd(cmd, rest)[0]
File "C:\Python33\lib\ftplib.py", line 742, in ntransfercmd
conn, size = FTP.ntransfercmd(self, cmd, rest)
File "C:\Python33\lib\ftplib.py", line 343, in ntransfercmd
source_address=self.source_address)
File "C:\Python33\lib\socket.py", line 424, in create_connection
raise err
File "C:\Python33\lib\socket.py", line 415, in create_connection
sock.connect(sa)
TimeoutError: [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
any advice would be greatly appreciated,
After looking at the ftplib source, it doesn't seem to want to use any port but 21.
I think you should be able to work around this, something like
import ftplib
ftplib.FTP.port = 995 # or whatever port you are using
ftps = ftplib.FTP_TLS('hostname', 'user', 'pwd')
ftps.retrlines('LIST')
Set the port through the connect
import ftplib
ftps = ftplib.FTP_TLS()
ftps.connect ('hostname', 991)

Categories

Resources