You are not logged in.

#1 2009-07-07 01:30:16

B4RR13N705
Member
Registered: 2009-06-08
Posts: 87

Problem with sockets under Python...

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. hmm


OS -----> Arch Linux     DE -----> KDE4
CPU ---> 2.66GHz         RAM ---> 512 MB
SWAP -> 2 G                / -------> 10 G
/home -> 50 G             /boot ---> 64 MB

Offline

#2 2009-07-07 02:04:43

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: Problem with sockets under Python...

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

#3 2009-07-07 02:46:09

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: Problem with sockets under Python...

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

#4 2009-07-08 19:41:35

B4RR13N705
Member
Registered: 2009-06-08
Posts: 87

Re: Problem with sockets under Python...

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

#5 2009-07-09 15:01:17

Mr.Elendig
#archlinux@freenode channel op
From: The intertubes
Registered: 2004-11-07
Posts: 4,092

Re: Problem with sockets under Python...

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

#6 2009-07-09 15:21:39

keenerd
Package Maintainer (PM)
Registered: 2007-02-22
Posts: 647
Website

Re: Problem with sockets under Python...

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

#7 2009-07-09 15:22:06

wuischke
Member
From: Suisse Romande
Registered: 2007-01-06
Posts: 630

Re: Problem with sockets under Python...

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

#8 2009-07-09 16:49:57

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: Problem with sockets under Python...

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

#9 2009-07-15 00:48:03

linkmaster03
Member
Registered: 2008-12-27
Posts: 269

Re: Problem with sockets under Python...

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

Board footer

Powered by FluxBB