파이썬 개발, 서버와 쓰레딩연계관한 질문입니다.

dlzhs의 이미지

제가 최근에 파이썬을 접해서 서버를 제작해보고 있는데 쓰레드를 사용해서 그냥 누가 접속 하면 접속 즉시 "(ip)가 접속하였습니다."라고 띄우는거 만들고 그와 동시에 실시간으로 명령어 입력받고 자시만의 서버 커맨드를 만들어서 실행 시키고 싶은데... 어떻게 해야하는지알려주세요. 프로그래밍 고수님 분들 도와주세요..ㅜㅜ

-----------------------------

import threading, socketserver, time

#서버 준비
ip = (접속 아이피)
port = 9999
server_contents = (ip, port)
online = True

class TCPAnswerHandle(socketserver.BaseRequestHandler):
def handle(self):
sock = self.request
sock.send("서버에 연결 되었습니다.".encode())
print(self.client_address[0], "가 연결되었습니다.")

class Server(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)

def run(self):
while online:
try:
print("서버가 성공적으로 열렸습니다.")
server = socketserver.TCPServer(server_contents, TCPAnswerHandle)
server.serve_forever()
except:
print("서버 연결에 실패 했습니다.")

server.shutdown()
server.server_close()

#시작
serv = Server()
serv.daemon = True
serv.start()

print("10초 후 실행 확인 됩니다." , end='')
for t in range(1, 11):
print('.', end='')
time.sleep(1)
print()
print("서버가 실행됩니다.")
while online:
command = input("Command > ")
time.sleep(0.1)
if command == "stop":
online = False
print("서버가 종료되었습니다.")


---------------------------

이렇게 만들었는데 입력 받는중이기 때문에 input()이 끝나기 전까지 위의 "(ip)가 접속 하였습니다."라는 문장이 않뜨고 이상태에서 커맨드를 실행시켜서 커맨드가 접속 된사람들에게 뭔가 할수 있게 하고 싶은데 class가 따로 있어서 접근이 불가능하고 그렇다고 global을 쓰니까 안되더라고요...ㅜㅜ 어떻게 해야하나요?

익명 사용자의 이미지

jick의 이미지

흠 좀 괴상한 구조인데요, 일단 본인이 하고 싶으신 게 뭔지 정확히 정리를 해야 할 것 같습니다.

그러니까 A가 접속하면 "A가 접속하였습니다"라고 뜨고, 서버를 띄운 사람이 명령어를 입력할 때까지 기다렸다가, 그 다음에 그 명령어를 실행한다는 건가요?

그런데 그 사이에 A가 접속을 끊어 버리거나 사용자 B, C가 더 접속하면 그 사람들은 어떻게 되죠? 명령어의 결과는 누구에게 가나요?

dlzhs의 이미지

일종에 마인크래프트 버킷을 열었을때의 마인크래프트 버킷의 상태 처럼 명령어를 칠수 있으면서 채팅시 서로에게 가는 구조의 서버를 만들려고해요.(프로그래밍을 독학으로 배운지라;; 기본도 모르고 모듈 사용법만 보고 배운지라...)

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.