Laboratory #1 IT System Security Lab University of Petroleum and Energy Studies
Laboratory #1 IT System Security Lab University of Petroleum and Energy Studies
Tutorial #1:
d. Using Dictionaries
Using list based dictionaries; you can attach values to the field names.
(i) >>> list1={'name': 'student1','age': 21,'batch': 'batch1'}
(ii) >>> list2={'name': 'student2','age': 22,'batch': 'batch2'}
(iii) >>> list1['name']
(iv) >>> list1['age']+=1
(v) >>> list1['age']
e. Nested Structures
(i) >>> list1={'name':{'first':'ADARSH','Last':'Kumar'},
'age':27,
'job':['senior','lecturer'],
'pay':(400,500)}
(ii) >>> list1['name']
(iii) >>> list1['name']['Last']
(iv) >>> db={}
(v) >>> db['list1']=list1
(vi) >>> db['list2']=list2
(vii) >>> db['list1']['list2']
(viii) >>> db['list1']
(ix) >>> db
3. >>> a=['hello']
4. >>> a[0]
5. >>> a[0][1]
6. >>> a[0][0]
7. >>> a[0][-1]
8. >>> a[0][-3]
9. File Handling
a. Writing data to a file
i. >>> file=open('data.txt','w')
ii. >>> file.write('Hello file world!\n')
iii. >>> file.write('Bye file world.\n')
iv. >>> file.close()
b. Reading data from a file
i. >>> file=open('data.txt','r')
ii. >>> for line in file.readlines():
iii. print line
iv. >>> file.seek(0)
v. >>> file.read()
vi. >>> file.seek(0)
vii. >>> file.readlines()
viii. >>> file.readline()
ix. >>> file.seek(0)
x. >>> file.readline()
xi. >>> file.seek(0)
xii. >>> file.read(1),file.read(8)
xiii. >>> file.next()
xiv. >>> file.next()
xv. >>> file.next()
c. Introduction to Socket programming functions
Basic functions that are used for socket programming are defined here. For these functions, you need to open python help
manual and study the functionality. At this stage functionality of these functions might not be very cleared to you but as you
will proceed and study, things will be clear.
i. >>> import socket
ii. >>> sock=socket.socket()
iii. >>> sock.bind(('localhost',8080))
iv. >>> sock.listen(3)
v. >>> sock1=socket.socket()
vi. >>> sock1.connect(('localhost',8080))
vii. >>> sock.close()
viii. >>> socket.gethostname()
ix. >>> socket.gethostbyname('localhost')
x. >>> socket.getfqdn()
xi. >>> socket.gethostbyaddr('127.0.0.1')
xii. >>> socket.gethostbyname_ex('localhost')
xiii. >>> socket.getprotobyname('TCP')
xiv. >>> socket.getservbyname('FTP')
xv. >>> socket.getservbyport(80)
xvi. >>> socket.has_ipv6
________________________________