Home CPSC 414

Lab 1: Cloud VM Setup

 

Objective

To set up a virtual machine (VM) on Google cloud. This will allow you to have a command-line machine for doing the labs and projects for this course.


 

Details

The instructions for setting up your Google Cloud account, and creating a virtual machine, are on the CPSC 225 page. You can find the instructions here:

http://ianfinlayson.net/class/cpsc225/notes/01-intro#google-cloud

Follow the linked instructions through the "Connecting with a Client" section.


 

Task

Once you have this setup, you should be able to use it to write and execute Python scripts. In order to test this, you should write a short script to display the current whether in Fredericksburg.

To do this open a file called lab1.py with Vim, and enter the following short program:


import urllib.request
import urllib.parse

url = "http://wttr.in/Fredericksburg?format=3"
data = urllib.request.urlopen(url)
print(data.read().decode("utf-8"))

This uses the Python urllib library which is for requesting data from URLs. This program uses the library to request data from the given URL and print it to the screen. wttr.in is a web service which provides weather information.

You should run this with the command:

$ python3 lab1.py
Fredericksburg: ☀️ +42⁰F

Your output will be different depending on the weather!

For this lab, you should change the program so that it asks the user to enter the location as input, and then display the weather in that location instead of always doing Fredericksburg. To do this, you'll need to do the following:

  1. Use the input function to get user input. This command takes the prompt to the user as an argument and returns the string they enter.
  2. Make the URL by concatenating three parts of the string. First the "http://wttr.in/", then the user input, and finally the "?format=3" part. In Python, + is for string concatenation.

When you run the program when you're done it should work like this:

$ python3 lab1.py
Enter a city: Sydney
Sydney: ⛅️ +73⁰F

$ python3 lab1.py
Enter a city: Anchorage
Anchorage: ☀️ +18⁰F

 

Submitting

When your program works, email the lab1.py file to ifinlay@umw.edu. Please attach it as a file instead of copying it into the body of the message!

If you need help getting the .py file off of your VM, you can see how to do it here:

http://ianfinlayson.net/class/cpsc225/notes/03-files2#accessing-files-remotely

Copyright © 2024 Ian Finlayson | Licensed under a Creative Commons BY-NC-SA 4.0 License.