show me how in the python requests library i can set a specific user-agent
Assistant
In the Python requests library, you can set a specific User-Agent using the headers parameter in your request. Here's how you can do it:
Setting User-Agent for a single request:
python
import requests
url = 'https://httpbin.org/user-agent'
headers = {
'User-Agent': 'my-app/0.0.1' # Set your desired User-Agent here
}
response = requests.get(url, headers=headers)
print(response.json())
Setting User-Agent for all requests in a session:
python
import requests
session = requests.Session()
session.headers.update({'User-Agent': 'my-app/0.0.1'}) # Set your desired User-Agent here
url = 'https://httpbin.org/user-agent'
response = session.get(url)
print(response.json())
In both examples, replace 'my-app/0.0.1' with the User-Agent string you want to use. The httpbin.org/user-agent endpoint is used here because it returns the User-Agent sent by the client in its response.
create a python function that takes as input a website name without the https part and checks if the certificate of that website is valid, return the number of days before the certificate will expire
Assistant
To create a Python function that checks the validity of a website's SSL certificate and returns the number of days before it expires, you can use the ssl module from the standard library. Here's a function that does this:
python
import socket
import ssl
from datetime import datetime
def get_ssl_expiration_days(domain):
context = ssl.create_default_context()
try:
with socket.create_connection((domain, 443)) as sock:
with context.wrap_socket(sock, server_hostname=domain) as ssock:
cert = ssock.getpeercert()
not_after = datetime.strptime(cert['notAfter'], "%b %d %H:%M:%S %Y %Z")
current_time = datetime.utcnow()
days_left = (not_after - current_time).days
return days_left
except Exception as e:
print(f"Error occurred: {e}")
return None
# Usage
domain = "example.com"
days_left = get_ssl_expiration_days(domain)
if days_left is not None:
print(f"The SSL certificate for {domain} will expire in {days_left} days.")
else:
print(f"Unable to retrieve SSL certificate information for {domain}.")
User
ok, now wrap this in a program, a single argument entered will be the website name, but also support -i to read the websites from a file
Assistant
Here's a Python script that wraps the previous function and supports both command-line arguments and reading website names from a file using the -i or --input-file flag:
python
User
add an commandline option -c to colorize output lines where the certificate is going to expire within 60 days
Assistant
To add colorization to the output for certificates expiring within 60 days, you can use ANSI escape codes. Here's the updated script with the -c or --color flag to enable colorization:
python
User
add a commandline -d to allow for specifying a different period than the 60 days
Assistant
To add the -d or --days flag to specify a custom period for colorizing output, update the script as follows:
python
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
import argparse
import socket
import ssl
from datetime import datetime
def get_ssl_expiration_days(domain):
context = ssl.create_default_context()
try:
with socket.create_connection((domain, 443)) as sock:
with context.wrap_socket(sock, server_hostname=domain) as ssock:
cert = ssock.getpeercert()
not_after = datetime.strptime(cert['notAfter'], "%b %d %H:%M:%S %Y %Z")
current_time = datetime.utcnow()
days_left = (not_after - current_time).days
return days_left
except Exception as e:
print(f"Error occurred for {domain}: {e}")
return None
def main():
parser = argparse.ArgumentParser(description="Check SSL certificate expiration days.")
parser.add_argument("domains", nargs="*", help="Domain names to check (optional)")
parser.add_argument("-i", "--input-file", help="Read domain names from a file")
args = parser.parse_args()
if args.input_file:
with open(args.input_file, "r") as file:
domains = [line.strip() for line in file if line.strip()]
else:
domains = args.domains
for domain in domains:
days_left = get_ssl_expiration_days(domain)
if days_left is not None:
print(f"The SSL certificate for {domain} will expire in {days_left} days.")
else:
print(f"Unable to retrieve SSL certificate information for {domain}.")
if __name__ == "__main__":
main()
To use this script, you can either pass domain names as command-line arguments or provide a file containing domain names one per line using the -i or --input-file flag.