Title: Test04- Requires Respondus LockDown Browser
Course: Intro to Scripting Language: Python (APT TEMPLATE) > Section: Test04

Test for Networking and Databases competency

Q: Assuming that the connection to the file named index.html at "www.dickbaldwin.com" is successful, the code in the top panel of Image 1 produces the output shown in the bottom panel of Image 1.

Image 1
import http.client



connection = http.client.HTTPConnection("www.dickbaldwin.com")

connection.request("GET", "/index.html")

response = connection.getresponse()

print("Status:",response.status, "Reason:", response.reason)



connection.close()

===============================================================================

Status: 200 Reason: OK
- True
- False
A: True
If right: See The Python Standard Library - 21.12. http.client - HTTP protocol client at https://docs.python.org/3/library/http.client.html

Q: Assuming that the connection to the file named index.html at "www.dickbaldwin.com" is successful, the code in the top panel of Image 2 produces the output shown in the bottom panel of Image 2 with minor variations that may change from one run to the next.

Image 2
import http.client



connection = http.client.HTTPConnection("www.dickbaldwin.com")

connection.request("GET", "/index.html")

response = connection.getresponse()

print("Status:",response.status, "Reason:", response.reason)

print()

print(response.getheaders())



connection.close()

===============================================================================

Status: 200 Reason: OK



Server: Apache/2.2

Content-Type: text/html; charset=iso-8859-1

Date: Sat, 08 Nov 2014 22:08:37 GMT

Content-Language: en-US

Accept-Ranges: bytes

Connection: Keep-Alive

Set-Cookie: X-Mapping-bffmijpk=2C686CCC2A6865C34A535895FE033586; path=/

Content-Length: 3285
- True
- False
A: False
If wrong: Assuming that the connection to the file named index.html at "www.dickbaldwin.com" is successful, the code in the top panel of Image 2 produces the output shown in Image 3 with minor variations that may change from one run to the next. Note that the data values are the same as in the bottom panel of Image 2 but the format is different. The problem is with the line that reads print(response.getheaders()) Image 3 Status: 200 Reason: OK [('Server', 'Apache/2.2'), ('Content-Type', 'text/html; charset=iso-8859-1'), (' Date', 'Sat, 08 Nov 2014 22:08:37 GMT'), ('Content-Language', 'en-US'), ('Accept -Ranges', 'bytes'), ('Connection', 'Keep-Alive'), ('Set-Cookie', 'X-Mapping-bffm ijpk=2C686CCC2A6865C34A535895FE033586; path=/'), ('Content-Length', '3285')]

Q: Assuming that the connection to the file named index.html at "www.dickbaldwin.com" is successful, the code in the top panel of Image 4 produces the output shown in the bottom panel of Image 4 with minor variations that may change from one run to the next.

Image 4
import http.client



connection = http.client.HTTPConnection("www.dickbaldwin.com")

connection.request("GET", "/index.html")

response = connection.getresponse()

print("Status:",response.status, "Reason:", response.reason)

print()

print(response.msg)



connection.close()

===============================================================================

Status: 200 Reason: OK



Server: Apache/2.2

Content-Type: text/html; charset=iso-8859-1

Date: Sat, 08 Nov 2014 22:15:55 GMT

Content-Language: en-US

Accept-Ranges: bytes

Connection: Keep-Alive

Set-Cookie: X-Mapping-bffmijpk=A6FAC0A1A16DBD577D9763B0D6D7684E; path=/

Content-Length: 3285
- True
- False
A: True
If right: Note that this output was produced with print(response.msg) instead of print(response.getheaders())

Q: Assuming that the connection to the file named index.html at "www.dickbaldwin.com" is successful, the code in the top panel of Image 5 produces the output shown in the bottom panel of Image 5 except that the actual date and time will change from one run to the next.

Image 5
import http.client



connection = http.client.HTTPConnection("www.dickbaldwin.com")

connection.request("GET", "/index.html")

response = connection.getresponse()

print("Status:",response.status, "Reason:", response.reason)

print()

print(response.getheader('Date'))



connection.close()

===============================================================================

Status: 200 Reason: OK



Sat, 08 Nov 2014 22:25:58 GMT
- True
- False
A: True

Q: Assuming that the connection to the file named index.html at "www.dickbaldwin.com" is successful, the code in the top panel of Image 6 produces the output shown in the bottom panel of Image 6 except that the contents of the file named index.html may change over time.

Image 6
import http.client



connection = http.client.HTTPConnection("www.dickbaldwin.com")

connection.request("GET", "/index.html")

response = connection.getresponse()

print("Status:",response.status, "Reason:", response.reason)

print()

print(response.read(410))



connection.close()

===============================================================================

Status: 200 Reason: OK



b'<?xml version="1.0" encoding="iso-8859-1"?>\r\n<!DOCTYPE html PUBLIC "-//W3C//

DTD XHTML 1.0 Transitional//EN"\r\n "http://www.w3.org/TR/xhtml1/DTD/xhtml1

-transitional.dtd">\r\n<html xmlns="http://www.w3.org/1999/xhtml">\r\n<head>\r\n

  <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />\r\n

  <title>Flex/ActionScript/C++/Scratch/Alice/C#/Java/JavaScript/XML\r\n Program

ming, by Richard G Baldwin'
- True
- False
A: True

Q: The code in the top panel of Image 7 produces the output shown in the bottom panel of Image 7.

Image 7
import dbm.dumb



dumbDb = dbm.dumb.open('database', 'c')



dumbDb[b'aNumber'] = 1234

print(dumbDb.get('aNumber'))



dumbDb.close()

===============================================================================

1234
- True
- False
A: False
If wrong: The code in Image 7 produces an error by attempting to store a number as a value. The output (with some text deleted for brevity) is shown in Image 8. Image 8 Traceback (most recent call last): File "1359-1930-11.py", line 11, in <module> dumbDb[b'aNumber'] = 1234 ... raise TypeError("values must be bytes or strings") TypeError: values must be bytes or strings

Q: The code in the top panel of Image 9 produces the output shown in the bottom panel of Image 9.

Image 9
import dbm.dumb



dumbDb = dbm.dumb.open('database', 'c')



dumbDb[b'aNumber'] = b'1234'

print(dumbDb.get('aNumber'))



dumbDb.close()

===============================================================================

b'1234'
- True
- False
A: True

Q: The code in the top panel of Image 10 produces the output shown in the bottom panel of Image 10.

Image 10
import dbm.dumb



dumbDb = dbm.dumb.open('database', 'c')



empName = b'Joe'



dumbDb[b'name'] = empName

dumbDb[b"cubicle"] = b"639"

dumbDb[b"phone"] = b"617.665.2154"



if(empName in dumbDb):

    print('name: ', dumbDb.get(b'name'))

    print('cubicle: ', dumbDb.get(b'cubicle'))

    print('phone: ', dumbDb.get(b'phone'))

else:

    print("Not in dumbDb")



dumbDb.close()

===============================================================================

name: b'Joe'

cubicle: b'639'

phone: b'617.665.2154'
- True
- False
A: False
If wrong: The output is shown in Image 11. The in operator searches for a match in the keys and does not search for a match in the values. Image 11 Not in dumbDb

Q: The code in the top panel of Image 12 produces the output shown in the bottom panel of Image 12.

Image 12
import dbm.dumb



dumbDb = dbm.dumb.open('database', 'c')



empName = b'Joe'



dumbDb[b'name'] = empName

dumbDb[b"cubicle"] = b"639"

dumbDb[b"phone"] = b"617.665.2154"



if(b'name' in dumbDb):

    print('name: ', dumbDb.get(b'name'))

    print('cubicle: ', dumbDb.get(b'cubicle'))

    print('phone: ', dumbDb.get(b'phone'))

else:

    print("Not in dumbDb")



dumbDb.close()

===============================================================================

name:  b'Joe'

cubicle:  b'639'

phone:  b'617.665.2154'
- True
- False
A: True

Q: The code in the top panel of Image 13 produces the output shown in the bottom panel of Image 13.

Image 13
import dbm.dumb



dumbDb = dbm.dumb.open('database', 'c')



myKeys = b'name',b'cubicle',b'phone',b'age',b'gender',b'marital'

myValues = b'Joe',b'639',b'333.444.5555',b'39',b'male',b'married'



for cnt in range(len(myKeys)):

    dumbDb[myKeys[cnt]] = myValues[cnt]



print(dumbDb.pop(b'phone','no phone'))

print(dumbDb.pop(b'weight','no weight'))



del dumbDb[b'cubicle']

print('Database length',len(dumbDb))

items = dumbDb.items()

for item in items:

    print(item)



dumbDb.close()

===============================================================================

b'333.444.5555'

b'169'

Database length 4

(b'name', b'Joe')

(b'marital', b'married')

(b'gender', b'male')

(b'age', b'39')
- True
- False
A: False
If wrong: The output is shown in Image 14. The bottom panel in Image 13 shows a value for a key named "weight" but the database doesn't have a key named "weight". Image 14 b'333.444.5555' no weight Database length 4 (b'name', b'Joe') (b'marital', b'married') (b'gender', b'male') (b'age', b'39')