I need to get the hostname of the unix server on which the application is running(maybe store in a properties file) and then fetch that hostname value in .py file and .html file.
I need the hostname for a url which is hardcoded in the code.
url should look something like this : http://{hostname value}.nam.net/
You can get the UNIX servers hostname by using the socket module:
import socket
socket.gethostname()
you can then write this to your configuration file if you need it to be static, or otherwise just re-use the code in your python files when generating the hostname.
I think that you will have a problem with what you want to do - the http://{hostname...}.nam.net will only work if you have a DNS services and something that will translate it from IP address to name.
maybe you want something like this:
import socket
def get_ip_address():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
return s.getsockname()[0]
this will open a socket to google server and you will get back your external ip address to be used as url
Related
Has anyone found a way to specify a DNS server for OpenSSL connections on a Linux OS? We have internal and external DNS servers and I am building a monitor for SSL certificate usage. I need the ability to specify a DNS server to be used on hostname connections. It works just fine against the internal DNS, but I am having difficulty finding a way to tie in a DNS server. I am fairly new to changing networks through Python and am not sure where to begin. Is it possible to do this through the dns.resolver module's nameservers function?
This looks like a viable solution for Windows, but I am hoping to find something similar for Linux.
How to Change DNS Servers Programmatically in Windows?
Below is my code that works against the default DNS host.
def readCerts(self,host,port,cast):
"""readCerts prompts terminal for username.
Attributes:
host: Host or IP of SSL connection
port: Port of SSL connection
cast: Format of returned results (JSON currently only structure supported)
Response:
Returns certificate attributes in specified format
"""
sslContext = SSL.Context(SSL.SSLv23_METHOD)
apiSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sslConnection = SSL.Connection(sslContext,apiSocket)
try:
sslConnection.connect((host,port))
except Exception as e:
raise e
else:
#Block the socket
sslConnection.setblocking(1)
#Set the hostname field for servers that support SNI. Format must be in bytestring.
sslConnection.set_tlsext_host_name(host.encode('utf-8'))
try:
sslConnection.do_handshake()
except:
pass
else:
#print "handshake succeeded"
sslConnection.close()
if cast.upper()=='JSON':
attributes = self._FormatJSON(sslConnection.get_peer_cert_chain())
return attributes
I am very novice with socket programming so sorry about any simple questions
My objective is to create a server which sends to several clients a csv file, clients located in different locations
I have a question about the following initial part of server and client
#TCP_IP = 'localhost'
TCP_IP = socket.gethostbyaddr("myip?")[0]
TCP_PORT = 9001
When debugging that line I get a valid isp domain but
If I do it this way I get an error that the Address is not valid
Shouldnt I place my real static ip in server script and in client script?
I've made this simple HTTP server in Python:
import SimpleHTTPServer
import SocketServer
PORT = 8000
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
print "serving at port", PORT
httpd.serve_forever()
What I want to do now, but I don't know how, is the following:
When I visit http://localhost:8000/my/path/download?documentID=100 , a document (particularly a PDF file) will start being downloaded automatically. Of course, I have more documents but I want them to be identified by the get paramater documentID
What is the easiest way to achieve this? thanks
You have to write a new handle. http://fragments.turtlemeat.com/pythonwebserver.php shows a simple example.
You have to:
determinate the PDF-File out of the ID
read the pdf file
send the right content-flag
send the pdf file
alternative:
write a script saving the pdfs with the ids like 1.pdf, 2.pdf
use your example code
and simple use links like http://localhost:8000/my/path/1.pdf
Greets Kuishi
PS: English isn't my native language, sorry
I am trying to get the original client ip address in a python cgi script. The client connect to the web server with a proxy. Following code always returns the proxy ip address. I tested all the env variable, HTTP_CLIENT_IP and HTTP_X_FORWARDED_FOR all return None. Is there any other way to get the client ip behind a proxy? like can I read the http header in a python cgi?
ipaddr = (getenv("HTTP_CLIENT_IP") or
getenv("HTTP_X_FORWARDED_FOR") or
getenv("REMOTE_ADDR") or
"UNKNOWN")
Do you want to:
find the client's IP address in the headers
use some sort of javascript to tell you?
For the former, you need the proxy to give you the client address.
Have you tried cgi.print_environ_usage() and looking for the IP address?
I am trying to log into a FTP server that I am only running locally in my network. To do this I have to use my ip address to use as the server address (see code below). However each time I get a gaierror: [Errno 11004] getaddrinfo failed error.
Can anyone look at my code and see if I am making any error that may be the cause of this address issue? Also I can log into the ftp server fine from my browser so I know the server is up and running correctly, also anonymous login to the server is allowed .
#import the ftp lib.
from ftplib import FTP
#enter the address of the ftp server to use, use ip address since server is ran locally
ftp = FTP('ftp://192.168.1.130')
#logs into the ftp server
ftp.login()
You're including the protocol with the hostname which is incorrect. What's happening is the library is trying to resolve "ftp://192.168.1.130" (instead of "192.168.1.130"), which isn't a valid address.
#Wrong
ftp = FTP('ftp://192.168.1.130')
#Right
ftp = FTP('192.168.1.130')
http://docs.python.org/library/ftplib.html
According to ftplib documentation, you shoud just give him the host address/IP, and not a string representing an URL. So here, you should simply do:
ftp = FTP('192.168.1.130')