-2

HELP please i have this code

import socket
from threading import *
import time
HOST = ''   # Symbolic name meaning all available interfaces
PORT = 8888 # Arbitrary non-privileged port

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print ('Socket created')
s.bind((HOST, PORT))     
print ('Socket bind complete')
s.listen(10)
print ('Socket now listening')
def listen(conn):
    odata = ""
    end = 'end'
    while end == 'end':
        data = conn.recv(1024)
        if data != odata:
            odata = data
            print(data)
            if data == b'end':
                end = ""
    print("conection ended")
    conn.close()
while True:
    time.sleep(1)
    conn, addr = s.accept()
    print ('Connected with ' + addr[0] + ':' + str(addr[1]))
    Thread.start_new_thread(listen,(conn))

and i would like it so that when ever a person comes onto the server it has its own thread. but i can't get it to work please someone help me. :_( here is the error code:

Socket created
Socket bind complete
Socket now listening
Connected with 127.0.0.1:61475
Traceback (most recent call last):
  File "C:\Users\Myles\Desktop\test recever - Copy.py", line 29, in <module>
    Thread.start_new_thread(listen,(conn))
AttributeError: type object 'Thread' has no attribute 'start_new_thread'

i am on python version 3.4.0 and here is the users code:

import socket   #for sockets
import time
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print('Socket Created')

host = 'localhost'
port = 8888
remote_ip = socket.gethostbyname( host )
print('Ip address of ' + host + ' is ' + remote_ip)
#Connect to remote server
s.connect((remote_ip , port))

print ('Socket Connected to ' + host + ' on ip ' + remote_ip)
while True:
    message = input("> ")
    #Set the whole string
    s.send(message.encode('utf-8'))
    print ('Message send successfully')
    data = s.recv(1024)
    print(data)
s.close
user3732790
  • 5
  • 1
  • 3

1 Answers1

2

The API you're using has moved from thread to _thread, so you'll need to do;

import _thread

The call is on the _thread module and requires a tuple as a second argument, so the correct line to start the thread would be;

_thread.start_new_thread(listen,(conn,))

IMHO, you're on the right track trying to use threading instead though, but that API works differently so you'll have to rewrite the code to make it work. A very brief description how it's used is for example available here.

Community
  • 1
  • 1
Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
  • it works but only one user can go on at once anyway to fix it? – user3732790 Jun 12 '14 at 09:00
  • @user3732790 Hm, I can connect multiple users without a problem and have them all work. What fails when you test it? – Joachim Isaksson Jun 12 '14 at 09:02
  • it only allows 1 person to join here is the code for the user – user3732790 Jun 12 '14 at 09:12
  • 'import socket #for sockets import time s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) print('Socket Created') host = 'localhost' port = 8888 remote_ip = socket.gethostbyname( host ) print('Ip address of ' + host + ' is ' + remote_ip) #Connect to remote server s.connect((remote_ip , port)) print ('Socket Connected to ' + host + ' on ip ' + remote_ip) while True: message = input("> ") #Set the whole string s.send(message.encode('utf-8')) print ('Message send successfully') data = s.recv(1024) print(data) s.close ' – user3732790 Jun 12 '14 at 09:12
  • @user3732790 Better to add a new question with the source correctly indented, right now I can't see if you're closing `s` inside or outside the loop for example. – Joachim Isaksson Jun 12 '14 at 09:18