Skip to content

Latest commit

 

History

History
125 lines (66 loc) · 3.27 KB

File metadata and controls

125 lines (66 loc) · 3.27 KB

Python_Networking

المحاضرة الاولى

import socket

عندي مجموعة اوامر استخدمه بالسوكت, منها :

1. عندي ال gethostbyname هذا انطي موقع دومين ويجيبلي من عنده الايبي, طبعا لازم الجهاز يكون متصل بالانترنيت

ip = socket.gethostbyname("www.google.com")

2. ايضا gethostbyaddr عكس الميثود الاولى, هاي انطيه الايبي وينطيني الدومين نيم

3. اكو getservbyname , هاي انطيه اسم البروتوكول وهي تنطيني رقم البورت لهذا البروتوكول مثلا http راح يطلعلي 80

4. اكو getservbyport هنا انطي رقم البورت وينطيني اسم البروتوكول الي يشتغل على هذا البورت مثلا بورت 21 لل FTP

            import socket

            #ip = socket.gethostbyname("www.google.com")

            #print(ip)

            #############################

            # host = socket.gethostbyaddr("142.250.75.36")

            # print(host)

            #############################

            #port = socket.getservbyname("http")

            #print(port)

            #############################

            service_name = socket.getservbyport(21)

            print(service_name)





المحاضرة الثانية: عمل server باستخدام بروتوكول UDP

اول شغله عندي لازم اعرف السوكيت يعني اعرف اساس الكونيكشن بالسيرفر على شنو يشتغل, socket.AF_INET معناها يشتغل على IPV4 و SOCK_DGRAM معناها يشتغل على UDP

s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
    import socket
	# server settings 
    s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)

	# set ip , and port
    s.bind(("127.0.0.1",5555))

	# set two variables, and set the buffer size for the variables
	# 1024 bytes = 1 kilo byte
	# why two variables ? recvfrom function return two data, one is the data the client sent which is Rdata, and the address will have the ip and port of the client
    Rdata , address = s.recvfrom(1024)

	# the data is coming encoded, so i net to decode the data
    data = Rdata.decode("UTF-8")  		# UTF-8 or ascii ...

	# Response from server to client 
    data_for_send = "Hello Client"

	# Encode the data that need to send to client
    Sdata = data_for_send,encoder("UTF-8") 

	# sendto function, send the data to client
    s.sendto(Sdata,address)


المحاضرة الثالثة: برمجة client

import socket

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

data_for_send = " Hello Server " 

Sdata = data_for_send.encode("UTF-8")

# sendto need two things, the data to be send, and the address 
s.sendto(Sdata,("127.0.0.1",5555))

Rdata , address = s.recvfrom(1024)

data = Rdata.decode("UTF-8")

print("Data Recieved From {} : \n {} ".format(address,data))



datetime

اذا اريد اجيب الوقت الحالي, لازم استدعي مكتبة datetime

from datetime import datetime

time = str(datetime.now())

print time