Pythontr

husonet | Tarih: 05.04.2011

python socket programlama örnek 1

Seriport'tan TCP IP'ye ceviren cihaz üzerinden dinleme yapmak için örnektir.

#-*- coding:utf-8 -*-
#!/usr/bin/python

#------------------------------------------------------------------------------
# Hüseyin Özdemir
# 05.04.2011
# husonet
# pythontr.com·
# Program seri haberleşme yapan cihazın TCP cevirici ile yapılmasına örnektir.
#------------------------------------------------------------------------------

import socket

ADRES = "192.168.0.100"
PORT = 101
SUNUCU = (ADRES, PORT)

try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(20)
sock.connect(SUNUCU)
#sock.send('GET %s HTML/1.0

' % (SAYFA))
while True:
try:
cevap = sock.recv(1024)
if not cevap: continue
print cevap
except:
pass

finally:
try:
sock.close()
except:
pass