When I run (WinXP OS, Python 2.7)
wd = webdriver.Remote (command_executor = 'http://127.0.0.1:4444/hub', desired_capabilities = webdriver.DesiredCapabilities.INTERNETEXPLORER)
in my system there is a proxy server by default, and is connected to selenium-server:4444 a through proxy. How to make that connection went directly to selenium-server:4444.
It is a bit late, but I stumbled over the same problem today and solved it, so for the next one who searches, here is the solution:
The system proxy settings are fetched from the *_proxy windows environment variables (http_proxy, https_proxy, ftp_proxy, ...), so if you have a company proxy defined there it will be used.
Add a new environment variable in windows options or, if you use intelliJ IDEA, in the Run configuration settings:
no_proxy=localhost,127.0.0.1
The reason you will find in python-2.7.6/Lib/urllib.py, around line 1387:
def proxy_bypass_environment(host):
"""Test if proxies should not be used for a particular host.
Checks the environment for a variable named no_proxy, which should
be a list of DNS suffixes separated by commas, or '*' for all hosts.
"""
no_proxy = os.environ.get('no_proxy', '') or os.environ.get('NO_PROXY', '')
# '*' is special case for always bypass
if no_proxy == '*':
return 1
# strip port off host
hostonly, port = splitport(host)
# check if the host ends with any of the DNS suffixes
no_proxy_list = [proxy.strip() for proxy in no_proxy.split(',')]
for name in no_proxy_list:
if name and (hostonly.endswith(name) or host.endswith(name)):
return 1
# otherwise, don't bypass
return 0
Related
I'm using various commands in Python to get the PC names:
platform.node()
socket.gethostname()
os.environ['COMPUTERNAME']
Is it possible to "mask" or "spoof" your PC name so that these commands get a pre-defined name? (not the real PC name)
As stated in the SMTPLIB doc :
class smtplib.SMTP(host='', port=0, local_hostname=None, [timeout, ]source_address=None) :
…
If specified, local_hostname is used as the FQDN of the local host in the HELO/EHLO command. Otherwise, the local hostname is found using socket.getfqdn()
I am using cx_oracle module with python 3.7 version and I need to check whether the connection is encrypted. If not I need to set the ssl as true in order to make it encrypted.
Here is my piece of code to make the connection:
import cx_Oracle
dsn = cx_Oracle.makedsn(host='127.0.0.1', port=1521, sid='your_sid')
conn = cx_Oracle.connect(user='your_username', password='your_password', dsn=dsn)
conn.close()
As you are thinking in enabling security to your connection, your first step should be to use a wallet, even before considering using ssl , and avoid using passwords. It does not matter how encrypted is your network traffic, if your passwords are visible in your Python programs. I know it is not part of the question itself, but it is a very good practice and available for cx_Oracle.
One example ( My Python programs runs in a Linux client machine which connects to an Oracle Database in Linux too using ssl )
Client Side
1.Create the wallet
mkstore -wrl "/home/myuser/wallet_directory" -create
2.Create the credential
mkstore -wrl "/home/myuser/wallet_directory" -createCredential mynetalias myuser myuserpw
Where mynetalias is an alias for my tns string connection which I will store on my tnsnames.ora file. In my example I will use the same directory where I created the wallet.
3.Create the tnsnames.ora and add the same alias used in the wallet
mynetalias =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = dbhost.example.com)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orclpdb1)
)
)
4.Create the sqlnet.ora file
WALLET_LOCATION =
(SOURCE =
(METHOD = FILE)
(METHOD_DATA =
(DIRECTORY = /home/myuser/wallet_dir)
)
)
SQLNET.WALLET_OVERRIDE = TRUE
5.Add your TNS_ADMIN environment variable to your bash profile.
cd
echo "export TNS_ADMIN=/home/myuser/wallet_directory" >> .bashrc
If you definitely know that the database server enforces integrity and encryption, then you do not need to configure anything in the client side. However you can also, or alternatively, do so depending on your business needs. Add the following lines to the sqlnet.ora file where the wallet is located
SQLNET.CRYPTO_CHECKSUM_CLIENT = required
SQLNET.CRYPTO_CHECKSUM_TYPES_CLIENT = (SHA512)
SQLNET.ENCRYPTION_CLIENT = required
SQLNET.ENCRYPTION_TYPES_CLIENT = (AES256)
Database Side
In order to setup SSL and encryption we need to add these values to the Database sqlnet.ora file. Review your requirements and discuss the right security algorithms. In my case my database accepts connection either way ( with or without encryption ).
SQLNET.CRYPTO_CHECKSUM_SERVER = accepted
SQLNET.CRYPTO_CHECKSUM_TYPES_SERVER = (SHA512)
SQLNET.ENCRYPTION_SERVER = accepted
SQLNET.ENCRYPTION_TYPES_SERVER = (AES256)
You might want to review these parameters here:
SQLNET Parameters
How to connect
Normal
connection = cx_Oracle.connect(dsn="mynetalias")
Pool
pool = cx_Oracle.SessionPool(externalauth=True, homogeneous=False,
dsn="mynetalias")
pool.acquire()
Remember that dsn must match exactly the alias used in your tnsnames.ora configured before.
Use the information provided by the view V$SESSION_CONNECT_INFO to assure your connection is encrypted ( field network_service_banner)
we can use python-oracledb driver which is the major version successor to cx_Oracle 8.3. https://python-oracledb.readthedocs.io/en/latest/user_guide/introduction.html
reference to the code: https://github.com/oracle/python-oracledb/discussions/34
I have a samba directory smb://172.16.0.10/public_pictures/ and I would like to know if it is accessible.
try something like the following:
import urllib
if open("smb://172.16.0.10/public_pictures/"):
print("accessible")
else:
print("no accessible")
but obviously it does not work for me
Using pysmb (docs):
from smb.SMBConnection import SMBConnection
remote_address = "172.16.0.10"
share_name = "public_pictures"
conn = SMBConnection(username, password, name, remote_name)
conn.connect(remote_address)
accessible = share_name in conn.listShares()
One way of handling samba is to use pysmb. If so, then it goes something like the following:
# we need to provide localhost name to samba
hostname = socket.gethostname()
local_host = (hostname.split('.')[0] if hostname
else "SMB{:d}".format(os.getpid()))
# make a connection
cn = SMBConnection(
<username>, <password>, local_host, <netbios_server_name>,
domain=<domain>, use_ntlm_v2=<use_ntlm_v2>,
is_direct_tcp=<self.is_direct_tcp>)
# connect
if not cn.connect(<remote_host>, <remote_port>):
raise IOError
# working connection ... to check if a directory exists, ask for its attrs
attrs = cn.getAttributes(<shared_folder_name>, <path>, timeout=30)
Some notes:
in your example above, public_pictures is the shared folder, while path would be simply /
you'll need to know if you are using SMB on port 139 or 445 (or a custom port). If the latter you will usually want to pass is_direct_tcp=True (although some servers will still serve NetBIOS samba on 445)
if you expect not to need a username or password, then probably you are expecting to connect as username="guest" with an empty password.
How to change the internet proxy settings using python in MacOS to set Proxy server and Proxy port
I do that with windows using this code:
import _winreg as winreg
INTERNET_SETTINGS = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r'Software\Microsoft\Windows\CurrentVersion\Internet Settings', 0, winreg.KEY_ALL_ACCESS)
def set_key(name, value):
_, reg_type = winreg.QueryValueEx(INTERNET_SETTINGS, name)
winreg.SetValueEx(INTERNET_SETTINGS, name, 0, reg_type, value)
set_key('ProxyEnable', 0)
set_key('ProxyOverride', u'*.local;<local>') # Bypass the proxy for localhost
set_key('ProxyServer', u'proxy.example.com:8080')
is this possible to do it on MacOS ?
After a long time of search, I found this way of how to change proxy on MacOs using python.
We need to use networksetup via terminal.
To set HTTP proxy server on MacOS using python:
import os
proxy = "proxy.example.com"
port = 8080
def Proxy_on():
os.system('networksetup -setwebproxy Ethernet '+proxy+' '+port)
Proxy_on()
and to turn it off:
import os
proxy = "proxy.example.com"
port = 8080
def Proxy_off():
os.system('networksetup -setwebproxystate Ethernet off')
Proxy_off()
If the network service isn't named just "Ethernet", you may need to parse networksetup -listallnetworkservices or -listnetworkserviceorder to get the correct name.
I know I can assign hosts with fabric by doing this:
env.hosts = ['host1', 'host2']
But can I do this?
myList = ['host1', 'host2']
env.hosts = myList
I am getting a list of 'public_dns_name's using Boto (from Amazon AWS) and then want to run commands on those servers. The server list can be dynamic so I need to be able to assign the hosts environment variable rather than statically. Can anyone suggest a solution?
myHosts = []
for i in myInstances:
publicDnsAddress = i.public_dns_name
myHosts.append(i.public_dns_name)
print ("public dns address: " + publicDnsAddress)
print ("myHosts = " + str(myHosts))
env.hosts=myHosts
env.user='myUser'
run("/scripts/remote_script.py")
I get this error:
No hosts found. Please specify (single) host string for connection:
If the host names were bad I would expect at least a connection error rather than a message saying it could find no hosts. Granted I may be calling this thing wrong but then again, that is why I am asking for help.
When dynamically setting hosts within the code, I've needed to use this with settings pattern:
user = 'root'
hosts = ['server1', 'server2']
for host in hosts:
with settings(user=user, host_string=host):
run(whatever_my_command_is)