Following is a python socket snippet:
import socket socket.socket(socket.AF_INET, socket.SOCK_STREAM)
My question is: does the line state whethet socket connection will be transported via TCP/IP? So far I was programming TCP connections only, using above line, but probably I was unaware of the fact. Am I able to program UDP connections using python sockets? How can I differentiate the transport layer?
The question isn't strictly connected to python, explanations are welcome as well in c++ or anything else.
The general syntax for creating a socket is:
socket(socket_family, socket_type, protocol=0)
We can use either AF_INET (for IPv4) or AF_INET6 (IPv6) as the first argument i.,e for socket_family.
The socket_type is the argument that determines whether the socket to be created is TCP or UDP. For TCP sockets it will be SOCK_STREAM and for UDP it will be SOCK_DGRAM (DGRAM - datagram). Finally, we can leave out the protocol argument which sets it to default value of '0'.
For TCP sockets you should have used bind(), listen() and accept() methods for server sockets and connect() or connect_ex() for client sockets. Whereas for UDP sockets you won't need listen(), accept() and connect() methods (as TCP sockets are connection-oriented sockets while UDP sockets are connection less sockets).
There are specific methods available for UDP to send and receive packets recvfrom() and sendto() respectively while recv() and send() are for TCP. Refer this documentation for socket for more information on respective methods for TCP and UDP. Also Core Python Applications Programming by Wesley Chun is a better book for some pretty basics on socket programming.
I have following code from wesley chun's core python application programming book
#!/usr/bin/env python """ tcp server """ from socket import AF_INET, SOCK_STREAM, socket from time import ctime HOST = '' PORT = 21567 BUFSIZE = 1024 ADDR = (HOST, PORT) tcp_server_socket = socket(AF_INET, SOCK_STREAM) tcp_server_socket.bind(ADDR) #maximum number of incoming connection before connection is refused tcp_server_socket.listen(5) while True: print "waiting for connection" tcpCliSock, addr = tcp_server_socket.accept() print "... connected from:" , addr while True: DATA = tcpCliSock.recv(BUFSIZE) if not DATA: break tcpCliSock.send('[%s] %s' % (ctime(), DATA)) tcpCliSock.close() tcp_server_socket.close()
I did some modifications to the original code however I am still confused how best to modify it to be more compliant
here are all the messages I am getting
C: 14,0: Invalid name "tcp_server_socket" (should match (([A-Z_][A-Z0-9_]*)|(__.*__))$) C: 21,4: Invalid name "tcpCliSock" (should match (([A-Z_][A-Z0-9_]*)|(__.*__))$) C: 21,16: Invalid name "addr" (should match (([A-Z_][A-Z0-9_]*)|(__.*__))$) E: 25,15: Instance of '_socketobject' has no 'recv' member E: 28,8: Instance of '_socketobject' has no 'send' member
I suppose first three just want me to use all caps variable names, is that the standard practice for these type of scripts, I don't see the code becoming more readable by using this convention, on the contrary it will look less readable, what are the motivation behind such rule in pylint and how to make code more compliant, I hardly think writer of such stature would write code like this without reason, be it readablity, beginner friendliness or anything else.
The two errors about
_socketobject you are seeing are a quirk of how the
socket module works. This issue has come up on StackOverflow once before, and the linked question provides a couple of answers to help you get rid of those errors.
The first three messages you are getting are convention warnings. They are complaining that the names
addr do not match the regular expression for constant members. Because your code is at 'top-level' (i.e. outside of any functions or classes), members are expected to be constant, and names of constants should match the regular expression given.
Suppose your Python script was saved in a file
tcp_server.py. If you then write
import tcp_server either from the Python interpreter or from another Python script, your TCP server will start. This isn't typically what you would want to happen. If you
import a module, it can define functions, classes and constants, but it shouldn't run any code.
I'd recommend moving all of the code from the line
tcp_server_socket = socket(....) downwards into a separate function, (let's call it
start_server), and then adding to the bottom of your script the following lines:
if __name__ == "__main__": start_server()
These two lines will then start your server if you run the script directly, but not if you
import tcp_server from somewhere else.
Once you've done that, the warnings about variable names will go, but you will get some further convention warnings. Two of them will complain about
tcpCliSock not matching the naming convention for variable names, and the other will nag you that your
start_server function doesn't have a docstring.