You are not logged in.
Hi, ive into Python last week and its a really nice programming lenguage.
I was just trying to make something productive with sockets XD
I thought about a messenger in terminal little script.
I wanted to do this:
In one open xterm, the server script is running and waiting.
In another xterm, the client script is running, and send messages to the server, the server listen to the message and print it in the screen.
But, i cant get it work. Both scripts connect succesfully but when the client sends a message, the server shows an error saying that socket_c is not defined and close.
Here are the sources:
SERVER:
import socket
socket_s = socket.socket()
host = raw_input("> Ingrese el HOST: ")
port = raw_input("> Ingrese el PORT: ")
socket_s.bind(("localhost", 9999))
socket_s.listen(1)
socket_s, ("localhost", 9999)
socket_s.accept()
received = socket_c.recv(1024)
quit = False
while not quit:
if received == "bye":
quit = True
else:
print received
CLIENT:
import socket
host = raw_input()
port = raw_input()
socket_c = socket.socket()
socket_c.connect(("localhost", 9999))
quit = False
while not quit:
sendto = raw_input()
if sendto == "bye":
quit = True
else:
socket_c.send(sendto)
I cant locate the error.
OS -----> Arch Linux DE -----> KDE4
CPU ---> 2.66GHz RAM ---> 512 MB
SWAP -> 2 G / -------> 10 G
/home -> 50 G /boot ---> 64 MB
Offline
in your server script you are referring to socket_c.receive, but you want to refer to socket_s.receive -- change the c to an s.
Dusty
Offline
Actually, I was a bit mistaken, leave it at socket_c, but change the line above to:
socket_c, address = socket_s.accept()
What you're doing in accept is getting a socket that's connected to the client socket on the other end. In theory the first socket can then keep on listening, although since you're on the same thread, this wouldn't actually happen.
You'll also want to add a
received = client_socket.recv(1024)
inside the loop so you can accept the next bit of data from the client.
Dusty
Last edited by Dusty (2009-07-07 02:47:41)
Offline
I did this
socket_c, address = socket_s.accept()
and it works.
Now im trying to figure out how to make the server a client too.
I want to make the server able to send messages too, and the client to received.
import socket
socket_s = socket.socket()
socket_c = socket.socket()
socket_s.bind((localhost, 9999))
socket_s.listen(1)
socket_s, (localhost, 9999)
socket_c, localhost = socket_s.accept()
socket_c.connect((localhost, 9999))
quit = False
while not quit:
received = socket_c.recv(1024)
if received == "bye":
quit = True
sendto = raw_input("> ")
if sendto == "bye":
quit = True
else:
socket_c.send(sendto)
else:
print received
sendto = raw_input("> ")
if sendto == "bye":
quit = True
else:
socket_c.send(sendto)
OS -----> Arch Linux DE -----> KDE4
CPU ---> 2.66GHz RAM ---> 512 MB
SWAP -> 2 G / -------> 10 G
/home -> 50 G /boot ---> 64 MB
Offline
As a sidenote, you probably want to use twisted instead of sockets, unless you are just using them to learn what they are.
Evil #archlinux@libera.chat channel op and general support dude.
. files on github, Screenshots, Random pics and the rest
Offline
Twisted seems a bit overkill. Python's SocketServer is fairly powerful and much easier to use than raw sockets. I've got a tutorial here:
http://kmkeen.com/socketserver/
Probably my favorite feature about SocketServer is the ease of multithreading.
Offline
Sorry for hijack, but this interests me:
Why would I want to use twisted? Which advantages does it offer compared to asynchat, for instance?
Offline
I can't imagine anyone ever voluntarily using twisted. If you like Python, twisted is the anti-python architecture. If you like the Twisted architecture, you should be using a less procedural language than python.
Dusty
Offline
Oddly enough, I did something exactly like this a few days ago. The thing is that the server has to send something, then the client, then the server, etc. Post here what you come up with.
serv.py
#!/usr/bin/env python
# Start this before chat.py
import socket
chat = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
chat.bind(('', 2735))
chat.listen(1)
recvmsg = ''
while 1:
pipe, details = chat.accept()
msg = raw_input('server: ')
pipe.send(msg)
recvmsg = pipe.recv(1024)
print 'client: ' + recvmsg
chat.py
#!/usr/bin/env python
import socket
chat = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
chat.connect(('localhost', 2735))
recvmsg = ''
while 1:
recvmsg = chat.recv(1024)
print 'server: ' + recvmsg
msg = raw_input('client: ')
chat.send(msg)
chat.close()
chat = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
chat.connect(('localhost', 2735))
Last edited by linkmaster03 (2009-07-15 00:48:52)
Offline