What does this line means? s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)

Question:

What does this line mean?

s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)

What does this syntax mean socket.socket() and socket.AF_INET`?
Can’t we use just AF_INET and Stream as parameter?

import socket # for socket
import sys 
 
try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    print "Socket successfully created"
except socket.error as err:
    print "socket creation failed with error %s" %(err)
 
# default port for socket
port = 80
 
try:
    host_ip = socket.gethostbyname('www.google.com')
except socket.gaierror:
 
    # this means could not resolve the host
    print "there was an error resolving the host"
    sys.exit()
 
# connecting to the server
s.connect((host_ip, port))
 
print "the socket has successfully connected to google 
on port == %s" %(host_ip)
Asked By: Aayush

||

Answers:

Object names reside in a namespace – a space containing names. A module (for example, socket) has its own namespace. The syntax is:

namespace.name

So socket.socket means the socket name (which happens to be a function) from the socket module – the module name comes first then the function name. If we omit the namespace then it assumes the current one, which in a simple single file program is called __main__.

We can arrange it so we import names into our own namespace and don’t need to specify the module name, which is what you asked for:

from socket import *

but that’s dangerous for a couple of reasons and is called namespace pollution.

One is that we can’t easily determine where something comes from – the code you show is quite short and not typical.

The other reason is namespace collisions. What if two modules happen to both use the same name, for example closedown? The last one defined is the one which will be used – there will be no warning that one has masked the other because python is designed to be dynamic.

So we know that socket.socket comes from the socket module, and not from some module describing car tools or one concerning electrical circuits. If we want we can use all three in the same program, but we must specify the namespace first.

Unfortunately you will see from module import * quite a lot because people are lazy. You can get away with it in a small program but you would be taking a risk – over time programs only ever get bigger and more complicated, they never get smaller and simpler.

There are other ways to use import: you can restrict importing only certain names and you can create aliases, but you should learn more about programming before using them. They have their uses but when they are appropriate is a judgement decision.

Answered By: cdarke

Many of the Python standard libraries are fairly thin wrappers around the underlying system libraries. They expose many of the idiosyncrasies of the underlying OS facilities, and you have to be familiar with the underlying system to properly understand their semantics.

If you really want to understand sockets, there are many excellent introductions to the topic. Most of them will require some familiarity with C, which may be a bit of a distraction (but understanding the basics of C is probably also a good investment of your time if you expect to be spending more of it reading and writing code).

You could very well create a more pythonic replacement for the Python socket module with proper encapsulation of the underlying facilities. It is unclear whether it would serve any useful purpose, though. Most trivial uses of sockets get by with a small number of slightly opaque but common enough pieces of “copy/paste programming” that most readers will understand roughly what’s going on in the code; others are involved enough that they do require full access to, and understanding of, the underlying facility.

Answered By: tripleee

You have imported the socket module, so everything from that module that you use will have “socket.” in front of it.

So socket.socket() means run the socket() function from the socket module.

You have to write socket.AF_INET because AF_INET is also from the socket module, so this means get the AF_INET constant from the socket module. Similar logic applies for socket.SOCK_STREAM.

For more on sockets: https://docs.python.org/2/library/socket.html

Also, in terms of learning to code in general, copying code and then trying to understand it can work, but it is much more powerful to try to understand the underlying concepts and then write your own code.

Answered By: DarthVlader

https://docs.python.org/3.10/library/socket.html

class socket.socket(family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None)

AF_INET is a constant that represents the address family (i.e. IPv4).
SOCK_STREAM is a constant that represents the socket type (i.e. TCP).

Answered By: Ahmed Rami Berrahal
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.