Hello friends, today I'll try to describe Socket module as much as I can...
Thread Titles;
>>What is Socket module?
>>What is Socket type seperated to?
>>Socket methods and explanations.
>>A small example is shown.
What is Socket Module?
Socket module, provides statics, functions and related exceptions, various objects for creating fullly accrual network applications including client and server programs.
The parameters required for a socket object is as below.
s=socket.socket(socket_family, socket_type, protocol=0)
Socket family separates into 2 :
socket_family ;
1) AF_UNIX (For data transfer in the same host with UNIX)(linux)
2) AF_INET (INET for data transfer on Internet) (general)
Socket type separates into 2 :
socket_type;
1) SOCK_STREAM (For TCP data transfer)
2) SOCK_DGRAM (For UDP data transfer)
Protocol can be used as 0 at standart.
protocol = 0 (default)
SOCKET METHODS AND DESCRIPTIONS;
>> s.socket()
>> s.bind()
>> s.listen()
>> s.accept()
>> s.connect()
>> s.recv()
>> s.send()
>> s.recvfrom()
>> s.sendto()
>> s.close()
>> socket.gethostname()
DESCRIPTIONS;
>>> s.socket(family, type, [protocol])
Used for creating Socket...
Family parameter specifies which domain is the socket made for. Generally used are (AF_UNIX) for UNIX domain. For Internet domain it's AF_INET.
The protocol type to be used is determined via Type parameter. For TCP SOCK_STREAM, For UDP SOCK_DGRAM is used. Other valid options used are SOCK_RAW, SOCK_SEQPACKET and SOCK_RDM.
>>> s.bind()
It's used for connecting the Socket to an address,bind() method is used as a parameter (host,port) consisting of the address and port number to the which socket will be connected.
>>> s.listen
It used for listening connection,as a parameter it receives how many connections will be accepted. Atleast it must be 1.
>>> s.accept()
It is used for accepting connections,this method even accepts incoming passive connection which appears when the connection comes and waits until the connection arrives.
>>> s.connect()
This method is used for connecting to Socket...
Kod:
s.connect(("192.168.1.97", 1234))
>>> s.recv()
This method receives TCP message.
>>> s.send()
This message transmits TCP message.
>>> s.recvfrom()
This method receives UDP message.
>>> s.sendto()
This method transmits UDP message.
>>> s.close()
This method closes socket.
>>> s.gethostname()
This method shows/turns host computer name.
A small example is shown below:
https://paste.ubuntu.com/p/rrqxGgwgn4/
Source : https://www.turkhackteam.org/python/1941437-python-socket-modulu.html
Translator : Vilge234
Thread Titles;
>>What is Socket module?
>>What is Socket type seperated to?
>>Socket methods and explanations.
>>A small example is shown.
What is Socket Module?
Socket module, provides statics, functions and related exceptions, various objects for creating fullly accrual network applications including client and server programs.
The parameters required for a socket object is as below.
s=socket.socket(socket_family, socket_type, protocol=0)
Socket family separates into 2 :
socket_family ;
1) AF_UNIX (For data transfer in the same host with UNIX)(linux)
2) AF_INET (INET for data transfer on Internet) (general)
Socket type separates into 2 :
socket_type;
1) SOCK_STREAM (For TCP data transfer)
2) SOCK_DGRAM (For UDP data transfer)
Protocol can be used as 0 at standart.
protocol = 0 (default)
SOCKET METHODS AND DESCRIPTIONS;
>> s.socket()
>> s.bind()
>> s.listen()
>> s.accept()
>> s.connect()
>> s.recv()
>> s.send()
>> s.recvfrom()
>> s.sendto()
>> s.close()
>> socket.gethostname()
DESCRIPTIONS;
>>> s.socket(family, type, [protocol])
Used for creating Socket...
Family parameter specifies which domain is the socket made for. Generally used are (AF_UNIX) for UNIX domain. For Internet domain it's AF_INET.
The protocol type to be used is determined via Type parameter. For TCP SOCK_STREAM, For UDP SOCK_DGRAM is used. Other valid options used are SOCK_RAW, SOCK_SEQPACKET and SOCK_RDM.
>>> s.bind()
It's used for connecting the Socket to an address,bind() method is used as a parameter (host,port) consisting of the address and port number to the which socket will be connected.
Kod:
s.bind(("192.168.1.90,1234))
>>> s.listen
It used for listening connection,as a parameter it receives how many connections will be accepted. Atleast it must be 1.
Kod:
s.listen(2)
>>> s.accept()
It is used for accepting connections,this method even accepts incoming passive connection which appears when the connection comes and waits until the connection arrives.
>>> s.connect()
This method is used for connecting to Socket...
Kod:
s.connect(("192.168.1.97", 1234))
>>> s.recv()
This method receives TCP message.
>>> s.send()
This message transmits TCP message.
>>> s.recvfrom()
This method receives UDP message.
>>> s.sendto()
This method transmits UDP message.
>>> s.close()
This method closes socket.
>>> s.gethostname()
This method shows/turns host computer name.
A small example is shown below:
Kod:
#Basit bir server yazdık :) import socket host = socket.gethostname()#Local Cihazımızı Çağıralım... port = 12345 try:s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)#Socketimizi oluşturalım... print("socket oluşturuldu") s.bind((host, port))#bind metodu ile host ve portu bildiriyoruz. print("Socket {} nolu porta bağlandı".format(port)) s.listen(5)# client üzerinden gelecek bağlantıyı bekliyoruz.... print("Socket Dinleniyor...") except socket.error as bildirim: print("Hata:",bildirim) while True:# Client ile bağlantı kurulursa c, addr = s.accept()#Gelen istekleri kabul eden methodu çağırdık.... print('Gelen bağlantı:', addr) # Bağlanan client a mesaj gönderelim. mesaj = 'Bağlantı Başarılı :)' c.send(mesaj.encode('utf-8'))# Bağlantıyı sonlandıralım c.close()
https://paste.ubuntu.com/p/rrqxGgwgn4/
Source : https://www.turkhackteam.org/python/1941437-python-socket-modulu.html
Translator : Vilge234
Moderatör tarafında düzenlendi: