Monday, May 27, 2013

steps to connect as client to a tcp/ip server

Hi there, I'm a really newbye, I've followed the polls tutorial.
 I'm looking for a way to dinamically create web-pages that will send and receive message to a TCP/Ip server that use a custom protocol for communication.
I've got the python code that handle the communication and I kindly ask you some advice on the right way to do it:

1)create a model with the fields of data to be sent and received from server
2)create in the view: 1) function that handle the connection
                                 2) function that send data
                                 3) function that receive data
3) create a template with model's fields

Is this right?

Below the python code that is now standalone and working to connect to server.


#Socket client in python

import socket   #for sockets
import sys  #for exit

#create an INET, STREAMing socket
try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error:
    print 'Failed to create socket'
    sys.exit()

print 'Socket Created'

host = '192.168.1.1';
port = 35000;

try:
    remote_ip = socket.gethostbyname( host )

except socket.gaierror:
    #could not resolve
    print 'Hostname could not be resolved. Exiting'
    sys.exit()

#Connect to remote server
s.connect((remote_ip , port))

print 'Socket Connected to ' + host + ' on ip ' + remote_ip

## SEND DATA
message = "GET / HTTP/1.1\r\n\r\n"

try :
    #Set the whole string
    s.sendall(message)
except socket.error:
    #Send failed
    print 'Send failed'
    sys.exit()

print 'Message send successfully'

#RECEIVE DATA
reply = s.recv(4096)

print reply

s.close()


Please let me know about it. Thanks. BR. 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment