24680 portu dinleyen ve gelen bilgileri ekrana yazan sunucu bazlı bir uygulama aşağıdaki gibi hazırlanabilir.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# Baslangic import socket sunucuSoketi = socket.socket(socket.AF_INET, socket.SOCK_STREAM) host = '' port = 23456 Buffer_Boyutu = 1024 sunucuSoketi.bind((host, port)) sunucuSoketi.listen(5) print "\n" + port, "portu acildi ve baglantilar dinleniyor" + "\n" baglantiAdedi = 1 while True: print "\n" + "*******************************************" + "\n" print baglantiAdedi, "nolu baglanti bekleniyor...." baglanti, istemciIPAdresi = sunucuSoketi.accept() # Baglanti talebi olusturuldu print "istemciden gelen", baglantiAdedi, "nolu baglanti kabul edildi" print 'Baglanan istemci IP Adresi ve Portu:', istemciIPAdresi print "Istemciden mesaj alinmasi bekleniyor..." while True: istemcidenGelenMesaj = baglanti.recv(Buffer_Boyutu) if not istemcidenGelenMesaj: break print "Istemciden mesaj geldi: ", istemcidenGelenMesaj print "Istemciden mesaj alindi ve Buffer bosaldi.", baglantiAdedi, "nolu istemci ile baglanti kesiliyor..." baglanti.send(str(baglantiAdedi)+'. baglantiyi kuran istemciydiniz. Baglandiginiz icin tesekkurler. Baglanti kesiliyor...') baglanti.close() print baglantiAdedi, "nolu istemci ile baglanti kesildi." baglantiAdedi += 1 # Sonuc |
Çalıştırıldığında sunucu olarak belirtilen bilgisayarın (192.168.2.200) 24680 portuna bağlanan; bilgisayarın IP adresini, portunu, adını ve bağlanan kullanıcının adını sunucuya gönderen bir uygulama aşağıdaki gibi hazırlanabilir.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#Baslangic import getpass kullaniciAdi = getpass.getuser() import socket bilgisayarAdi = socket.gethostname() bilgisayarIpAdresi = socket.gethostbyname(socket.gethostname()) # from time import gmtime, strftime # strftime("%Y-%m-%d %H:%M:%S", gmtime()) sunucuyaGonderilecekMesaj = kullaniciAdi+"___"+bilgisayarAdi+"___"+bilgisayarIpAdresi import socket istemciSoketi = socket.socket(socket.AF_INET, socket.SOCK_STREAM) host = "192.168.2.200" port = 23456 Buffer_Boyutu = 1024 istemciSoketi.connect((host, port)) print "Gonderilecek veri: ", sunucuyaGonderilecekMesaj istemciSoketi.send(sunucuyaGonderilecekMesaj) print "Veri sunucuya basarili bir sekilde gonderildi." sunucudanGelenMesaj = istemciSoketi.recv(Buffer_Boyutu) print "Sunucudan Gelen Mesaj: ", sunucudanGelenMesaj print "Sunucudan onay mesaji da alindigina gore; istemci tarafinda da baglanti koparilabilir" istemciSoketi.close() # Sonuc |
Bu uygulama istemcide çalıştırıldığında aşağıdaki gibi bir çıktı görülür.
Bunun yanında sunucu tarafında ise aşağıdaki gibi bir çıktı elde edilebilir.
Veri transferi yerine dosya transferi de gerçekleştirilebilir. Python ile dosya transferi gerçekleştirmek için bakınız:
http://www.bogotobogo.com/python/python_network_programming_server_client_file_transfer.php
Bu koddaa veriler byte cinsinde gönderiliyor o yüzden hata aldım