Help with Basic Python sockets

Status
Not open for further replies.

dpowers

Ars Scholae Palatinae
1,395
Subscriptor
So I'm using O'Reilly's book to learn about Python sockets, and I have my server and client ready:

echo-server.py
code:<HR>
Code:
from socket import *	
myHost = ''					
myPort = 50007		
sockobj = socket(AF_INET, SOCK_STREAM)
sockobj.bind((myHost, myPort))		
sockobj.listen(5)					<P>while 1:									
	connection, address = sockobj.accept()	
	print 'Server connected by', address
	while 1:
		data = connection.recv(1024)	
		if not data: break			
		connection.send('Echo=>' + data)
	connection.close()

echo-client.py

code:<HR>
Code:
import sys
from socket import *	
serverHost = 'localhost'	
serverPort = 50007			<P>message = ['Hello network world']	
if len(sys.argv) > 1:
	serverHost = sys.argv[1]		
	if len(sys.argv) > 2:			
		message = sys.argv[2:]	
		
sockobj = socket(AF_INET, SOCK_STREAM)	
sockobj.connect((serverHost, serverPort))<P>for line in message:
	sockobj.send(line)			
	data = sockobj.recv[1024]	
	print 'Client received:', `data`<P>sockobj.close()

When I run this program in Cygwin, first I start echo-server.py (python echo-server.py). Then I start echo-client.py. As soon as I start it, I get the following messages:

echo-server.py:
Server connected by ('127.0.0.1', 4199)
Traceback (most recent call last):
File "echo-server.py", line 21, in ?
data = connection.recv(1024)
socket.error: (104, 'Connection reset by peer')

echo-client.py:
Traceback (most recent call last):
File "echo-client.py", line 28, in ?
data = sockobj.recv[1024]
TypeError: unsubscriptable object


Does anyone know what I'm doing wrong here? I just want the server to echo back that little blurb of text.

-tig
 

green_buddy

Wise, Aged Ars Veteran
126
Ok... I'll take a stab at this. Although, bear with me, 'cause I'm not a python programmer... but from just looking at things, is it possible that both the client and the server are trying to listen on the same port, on the same machine? 'Cause it looks like they might be trying to listen on the same port.

Maybe I need to read up on Python, or atleast grab that O'Reilly book.

-green
 
Status
Not open for further replies.