My code
I've gone over it and had a friend of mine look at it. At this point we're both stumped and would appreciate some help.
import os
import socket
import ssl
HSM = raw_input('Please enter the IP address of the machine you are connecting to.')
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sslSocket = ssl.wrap_socket(sock,
keyfile="AKMClientPrivateKey.pem",
certfile="AKMClientSignedCert.pem",
ca_certs="TCASelfSignedCert.pem",
cert_reqs=ssl.CERT_REQUIRED)
sslSocket.connect(('192.168.160.128', 6000))
print "Connection is successful!"
The error I keep getting
I have all of the keys and certs I'm referencing in the same folder as my python file.
I've double checked the IP address and port.
Traceback (most recent call last):
File "connect.py", line 12, in <module>
sslSocket.connect(['192.168.160.128', 6000])
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 333, in connect
self._real_connect(addr, False)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 314, in _real_connect
self.ca_certs, self.ciphers)
ssl.SSLError: [Errno 336265218] _ssl.c:351: error:140B0002:SSL
routines:SSL_CTX_use_PrivateKey_file:system lib
Related
I am trying to access the open DLP Test FTP server as a practice. I keep getting a getaddrinfo error but I am unsure of where I'm going wrong. I am using Python 2 on a Windows 10, and have already checked that I am not behind a proxy.
Code:
from ftplib import FTP
ftp = FTP('ftp://ftp.dlptest.com/')
...
Error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\ftplib.py", line 120, in __init__
self.connect(host)
File "C:\Python27\lib\ftplib.py", line 135, in connect
self.sock = socket.create_connection((self.host, self.port), self.timeout)
File "C:\Python27\lib\socket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 11001] getaddrinfo failed
Any help is appreciated!
Use
ftp = FTP('ftp.dlptest.com')
instead.
The first argument of FTP constructor is host – a hostname or an IP address – not a URL.
Hello I would ask if i could do connection to extract some data from Tool Server.
so i making this code but i need to modify it to open this tool with username and password and extract data from My tool server.
import socket
import ssl
HOST, PORT = '10.74.159.82', 31039
def handle(conn):
conn.write(b'GET / HTTP/1.1\n')
print(conn.recv() . decode())
def main():
sock = socket.socket(socket.AF_INET)
context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)
conn = context.wrap_socket(sock, server_hostname=HOST)
try:
conn.connect((HOST, PORT))
handle(conn)
finally:
conn.close()
if __name__ == '__main__':
main()
RESULT
!! C:\Users\Admin\.PyCharmCE2018.1\config\venv\Scripts\python.exe!! C:/Users/Admin/.PyCharmCE2018.1/config/codestyles/Under_Building.py
Traceback (most recent call last):
File "C:/Users/Admin/.PyCharmCE2018.1/config/codestyles/Under_Building.py", line 22, in <module>
main()
File "C:/Users/Admin/.PyCharmCE2018.1/config/codestyles/Under_Building.py", line 16, in main
conn.connect((HOST, PORT))
File "C:\Users\Admin\AppData\Local\Programs\Python\Python37-32\lib\ssl.py", line 1141, in connect
self._real_connect(addr, False)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python37-32\lib\ssl.py", line 1132, in _real_connect
self.do_handshake()
File "C:\Users\Admin\AppData\Local\Programs\Python\Python37-32\lib\ssl.py", line 1108, in do_handshake
self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1045)
Process finished with exit code 1
I'm Beginner so i need to learn and supporting
It's hard to see exactly, but it seems the server you're connecting to (on https://10.74.159.82:31039) is using a self-signed certificate (or its CA is, perhaps).
It's perhaps better to create or buy a proper (non-self-signed) certificate, but assuming this is a test server this is fine.
So with what you have, your best bet is to allow self-signed certs in your SSL Context (and also, not to check the hostname - that will probably fail too as you're using an IP address):
context = ssl.create_default_context()
context.check_hostname=False
context.verify_mode=ssl.CERT_NONE
# etc...
I've been trying to connect to my Gmail account using python. imap is enabled.
import imaplib
imap_server = imaplib.IMAP4_SSL("imap.gmail.com",993)
# also tried imap_server = imaplib.IMAP4_SSL("imap.gmail.com"), doesnt work.
Traceback is :
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
imap_server = imaplib.IMAP4_SSL("imap.gmail.com",993)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 1202, in __init__
IMAP4.__init__(self, host, port)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 172, in __init__
self.open(host, port)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 1217, in open
IMAP4.open(self, host, port)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 248, in open
self.sock = self._create_socket()
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 1205, in _create_socket
sock = IMAP4._create_socket(self)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 238, in _create_socket
return socket.create_connection((self.host, self.port))
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/socket.py", line 435, in create_connection
raise err
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/socket.py", line 426, in create_connection
sock.connect(sa)
OSError: [Errno 65] No route to host
What OSError: [Errno 65] No route to host means is what it say: you can't get to that machine from your machine.
You can test that from outside of Python by opening up a terminal/DOS prompt and typing this:
ping imap.gmail.com
It's possible that this is actually a name lookup error, and you're somehow getting a bad address for imap.gmail.com. So, just to be sure, check by IP address too:
ping 74.125.129.108
ping 74.125.129.109
If ping works, you can check whether your router is for some reason just blocking TCP access to the host, e.g., with:
telnet imap.gmail.com
If it's working, this should either hang for a long time, or give you a connection-refused error; if it gives you a no-route-to-host error, it's the same problem you're seeing.
It's also possible that your router is specifically blocking port 993. You can test this too:
telnet imap.gmail.com 993
If it doesn't come back with something like "Connected to gmail-imap.l.google.com", same problem here too.
At any rate, once you've verified that this is a system or network configuration problem, not a programming problem, go ask for help with your system on the appropriate site.
I'm really new to networking in general and I'm trying to set up a simple exchange between a python server and client.
This is the code for the server
import socket, ssl
def do_something(connstream, data):
print "HALLO"
def deal_with_client(connstream):
data = connstream.read()
# null data means the client is finished with us
while data:
if not do_something(connstream, data):
# we'll assume do_something returns False
# when we're finished with client
break
data = connstream.read()
# finished with client
bindsocket = socket.socket()
bindsocket.bind(('127.0.0.1', 10024))
bindsocket.listen(5)
while True:
newsocket, fromaddr = bindsocket.accept()
print "Setting up connection"
connstream = ssl.wrap_socket(newsocket,
server_side=True,
ca_certs=None,
certfile="cert.pem",
keyfile="privatekey.pem",
ssl_version=ssl.PROTOCOL_TLSv1)
try:
deal_with_client(connstream)
finally:
connstream.shutdown(socket.SHUT_RDWR)
connstream.close()
Here is the code for the client.py
import socket, ssl
clientsocket = socket.socket()
ssl_sock = ssl.wrap_socket(clientsocket,
certfile="cert.pem",
cert_reqs=ssl.CERT_REQUIRED)
ssl_sock.connect(('127.0.0.1', 10024))
print ssl_sock.getpeername()
print ssl_sock.getpeercert()
data = ssl_sock.recv(1024)
ssl_sock.close()
print 'Received', repr(data)
I generated the "cert.pem" and the "privatekey.pem" using openssl.
Traceback (most recent call last):
File "server.py", line 30, in <module>
ssl_version=ssl.PROTOCOL_TLSv1)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 344, in wrap_socket
ciphers=ciphers)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 121, in __init__
self.do_handshake()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 283, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [Errno 8] _ssl.c:499: EOF occurred in violation of protocol
I was wondering if somehow who knows more could point me in the right direction. I really want to do this using SSL btw, but I would be willing to switch to TLS if that is the better approach.
It could be that the sockets are not running with a compatible ssl version, you should put a "ssl.PROTOCOL_TLSv1" compatible version in your client too (or removing it from server and use the default value too).
Googling you can find many examples of socket comunication
I am doing the following
>> from ftplib import FTP
>> s = FTP('host','user','password') # Connect
and it fails giving the following
Traceback (most recent call last): File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ftplib.py", line 117, in __init__
self.connect(host)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ftplib.py", line 132, in connect
self.sock = socket.create_connection((self.host, self.port), self.timeout)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 571, in create_connection
raise err socket.error: [Errno 60] Operation timed out
I know that host, user, passwd are correct
How do I debug/fix this error?
WinSCP (which you've otherwise been using to connect to the same server) supports SFTP and SCP, not FTP.
To write a Python program using SFTP, you should be using the Paramiko library.
Try doing it like this:
try:
s = FTP(host)
s.login(user, password)
except Exception, e:
print "The error was:", str(e)