How do you implement socket programming in python? (AI) Flashcards

(20 cards)

1
Q

What Python module is the core of socket programming?

A

The socket module.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the necessary first line of code to use sockets in a script?

A

import socket

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What function is used to create a new socket object?

A

socket.socket()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What argument in socket.socket() specifies the IPv4 address family?

A

socket.AF_INET (Address Family: Internet)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What argument specifies the connection-oriented TCP protocol?

A

socket.SOCK_STREAM (Stream Socket)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What argument specifies the connectionless UDP protocol?

A

socket.SOCK_DGRAM (Datagram Socket)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the very first mandatory step for a server socket?

A

s.bind((host, port)) (Assigning its address and port)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

For s.bind(), what host value listens on all available network interfaces?

A

'0.0.0.0' or an empty string ('')

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

For s.bind(), what host value listens only on the local machine (localhost)?

A

'127.0.0.1'

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What method puts the server socket into passive listening mode?

A

s.listen(backlog)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does the backlog argument in s.listen() control?

A

The maximum number of queued (unaccepted) client connections.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What method blocks and waits for a new client connection on the server?

A

conn, addr = s.accept()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What two objects does s.accept() return?

A

A new connection socket (conn) and the client’s address tuple (addr).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What method does a client use to initiate a connection to a server?

A

s.connect((host, port))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What method sends data over a connected socket?

A

conn.send(data) (or s.send())

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What method receives data, and what argument specifies the buffer size?

A

conn.recv(bufsize) (The bufsize is the max number of bytes to receive)

17
Q

What format must data be in before being sent with s.send()?

A

Bytes (b'...'). You must use .encode().

18
Q

What must you do with the data received from s.recv() to read it as text?

A

Use the .decode() method (e.g., data.decode('utf-8')).

19
Q

What method is used by both client and server to properly release the socket resource?

20
Q

What Python keyword is highly recommended for automatically managing the closing of a socket?

A

The with statement (used for reliable cleanup).