PageRenderTime 28ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/Languages/IronPython/Tests/modules/network_related/socket_test.py

https://github.com/thomo13/ironruby
Python | 651 lines | 609 code | 15 blank | 27 comment | 3 complexity | aa305a5313692ac71d48880b0c1eab78 MD5 | raw file
  1. #####################################################################################
  2. #
  3. # Copyright (c) Microsoft Corporation. All rights reserved.
  4. #
  5. # This source code is subject to terms and conditions of the Apache License, Version 2.0. A
  6. # copy of the license can be found in the License.html file at the root of this distribution. If
  7. # you cannot locate the Apache License, Version 2.0, please send an email to
  8. # ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. # by the terms of the Apache License, Version 2.0.
  10. #
  11. # You must not remove this notice, or any other, from this software.
  12. #
  13. #
  14. #####################################################################################
  15. #
  16. # test socket
  17. #
  18. from iptest.assert_util import *
  19. skiptest("silverlight")
  20. import sys
  21. import thread
  22. import time
  23. #workaround - _socket does not appear to be in $PYTHONPATH for CPython
  24. #only when run from the old test suite.
  25. try:
  26. import socket
  27. except:
  28. print "Unable to import socket (_socket) from CPython"
  29. sys.exit(0)
  30. #-----------------------
  31. #--GLOBALS
  32. AF_DICT = {"AF_APPLETALK" : 5,
  33. "AF_DECnet" : 12,
  34. "AF_INET" : 2,
  35. "AF_INET6" : 10,
  36. "AF_IPX" : 4,
  37. "AF_IRDA" : 23,
  38. "AF_SNA" : 22,
  39. "AF_UNSPEC" : 0,
  40. }
  41. ST_DICT = {"SOCK_DGRAM" : 2,
  42. "SOCK_RAW" : 3,
  43. "SOCK_RDM" : 4,
  44. "SOCK_SEQPACKET" : 5,
  45. "SOCK_STREAM" : 1,
  46. }
  47. IPPROTO_DICT = { "IPPROTO_AH" : 51,
  48. "IPPROTO_DSTOPTS" : 60,
  49. "IPPROTO_ESP" : 50,
  50. "IPPROTO_FRAGMENT" : 44,
  51. "IPPROTO_HOPOPTS" : 0,
  52. "IPPROTO_ICMP" : 1,
  53. "IPPROTO_ICMPV6" : 58,
  54. "IPPROTO_IDP" : 22,
  55. "IPPROTO_IGMP" : 2,
  56. "IPPROTO_IP" : 0,
  57. "IPPROTO_IPV6" : 41,
  58. "IPPROTO_NONE" : 59,
  59. "IPPROTO_PUP" : 12,
  60. "IPPROTO_RAW" : 255,
  61. "IPPROTO_ROUTING" : 43,
  62. "IPPROTO_TCP" : 6,
  63. "IPPROTO_UDP" : 17,
  64. }
  65. OTHER_GLOBALS = {"AI_ADDRCONFIG" : 32,
  66. "AI_ALL" : 16,
  67. "AI_CANONNAME" : 2,
  68. "AI_NUMERICHOST" : 4,
  69. "AI_PASSIVE" : 1,
  70. "AI_V4MAPPED" : 8,
  71. "EAI_ADDRFAMILY" : -9,
  72. "EAI_AGAIN" : -3,
  73. "EAI_BADFLAGS" : -1,
  74. "EAI_FAIL" : -4,
  75. "EAI_FAMILY" : -6,
  76. "EAI_MEMORY" : -10,
  77. "EAI_NODATA" : -5,
  78. "EAI_NONAME" : -2,
  79. "EAI_SERVICE" : -8,
  80. "EAI_SOCKTYPE" : -7,
  81. "EAI_SYSTEM" : -11,
  82. "INADDR_ALLHOSTS_GROUP" : -536870911,
  83. "INADDR_ANY" : 0,
  84. "INADDR_BROADCAST" : -1,
  85. "INADDR_LOOPBACK" : 2130706433,
  86. "INADDR_MAX_LOCAL_GROUP" : -536870657,
  87. "INADDR_NONE" : -1,
  88. "INADDR_UNSPEC_GROUP" : -536870912,
  89. "IPPORT_RESERVED" : 1024,
  90. "IPPORT_USERRESERVED" : 5000,
  91. "IPV6_CHECKSUM" : 7,
  92. "IPV6_DSTOPTS" : 4,
  93. "IPV6_HOPLIMIT" : 8,
  94. "IPV6_HOPOPTS" : 3,
  95. "IPV6_JOIN_GROUP" : 20,
  96. "IPV6_LEAVE_GROUP" : 21,
  97. "IPV6_MULTICAST_HOPS" : 18,
  98. "IPV6_MULTICAST_IF" : 17,
  99. "IPV6_MULTICAST_LOOP" : 19,
  100. "IPV6_NEXTHOP" : 9,
  101. "IPV6_PKTINFO" : 2,
  102. "IPV6_RTHDR" : 5,
  103. "IPV6_RTHDR_TYPE_0" : 0,
  104. "IPV6_UNICAST_HOPS" : 16,
  105. "IPV6_V6ONLY" : 26,
  106. "IP_ADD_MEMBERSHIP" : 35,
  107. "IP_DEFAULT_MULTICAST_LOOP" : 1,
  108. "IP_DEFAULT_MULTICAST_TTL" : 1,
  109. "IP_DROP_MEMBERSHIP" : 36,
  110. "IP_HDRINCL" : 3,
  111. "IP_MAX_MEMBERSHIPS" : 20,
  112. "IP_MULTICAST_IF" : 32,
  113. "IP_MULTICAST_LOOP" : 34,
  114. "IP_MULTICAST_TTL" : 33,
  115. "IP_OPTIONS" : 4,
  116. "IP_RECVOPTS" : 6,
  117. "IP_RECVRETOPTS" : 7,
  118. "IP_RETOPTS" : 7,
  119. "IP_TOS" : 1,
  120. "IP_TTL" : 2,
  121. "MSG_CTRUNC" : 8,
  122. "MSG_DONTROUTE" : 4,
  123. "MSG_DONTWAIT" : 64,
  124. "MSG_EOR" : 128,
  125. "MSG_OOB" : 1,
  126. "MSG_PEEK" : 2,
  127. "MSG_TRUNC" : 32,
  128. "MSG_WAITALL" : 256,
  129. "NI_DGRAM" : 16,
  130. "NI_MAXHOST" : 1025,
  131. "NI_MAXSERV" : 32,
  132. "NI_NAMEREQD" : 8,
  133. "NI_NOFQDN" : 4,
  134. "NI_NUMERICHOST" : 1,
  135. "NI_NUMERICSERV" : 2,
  136. "PACKET_BROADCAST" : 1,
  137. "PACKET_FASTROUTE" : 6,
  138. "PACKET_HOST" : 0,
  139. "PACKET_LOOPBACK" : 5,
  140. "PACKET_MULTICAST" : 2,
  141. "PACKET_OTHERHOST" : 3,
  142. "PACKET_OUTGOING" : 4,
  143. "PF_PACKET" : 17,
  144. "SHUT_RD" : 0,
  145. "SHUT_RDWR" : 2,
  146. "SHUT_WR" : 1,
  147. "SOL_IP" : 0,
  148. "SOL_SOCKET" : 1,
  149. "SOL_TCP" : 6,
  150. "SOL_UDP" : 17,
  151. "SOMAXCONN" : 128,
  152. "SO_ACCEPTCONN" : 30,
  153. "SO_BROADCAST" : 6,
  154. "SO_DEBUG" : 1,
  155. "SO_DONTROUTE" : 5,
  156. "SO_ERROR" : 4,
  157. "SO_KEEPALIVE" : 9,
  158. "SO_LINGER" : 13,
  159. "SO_OOBINLINE" : 10,
  160. "SO_RCVBUF" : 8,
  161. "SO_RCVLOWAT" : 18,
  162. "SO_RCVTIMEO" : 20,
  163. "SO_REUSEADDR" : 2,
  164. "SO_SNDBUF" : 7,
  165. "SO_SNDLOWAT" : 19,
  166. "SO_SNDTIMEO" : 21,
  167. "SO_TYPE" : 3,
  168. "SSL_ERROR_EOF" : 8,
  169. "SSL_ERROR_INVALID_ERROR_CODE" : 9,
  170. "SSL_ERROR_SSL" : 1,
  171. "SSL_ERROR_SYSCALL" : 5,
  172. "SSL_ERROR_WANT_CONNECT" : 7,
  173. "SSL_ERROR_WANT_READ" : 2,
  174. "SSL_ERROR_WANT_WRITE" : 3,
  175. "SSL_ERROR_WANT_X509_LOOKUP" : 4,
  176. "SSL_ERROR_ZERO_RETURN" : 6,
  177. "TCP_CORK" : 3,
  178. "TCP_DEFER_ACCEPT" : 9,
  179. "TCP_INFO" : 11,
  180. "TCP_KEEPCNT" : 6,
  181. "TCP_KEEPIDLE" : 4,
  182. "TCP_KEEPINTVL" : 5,
  183. "TCP_LINGER2" : 8,
  184. "TCP_MAXSEG" : 2,
  185. "TCP_NODELAY" : 1,
  186. "TCP_QUICKACK" : 12,
  187. "TCP_SYNCNT" : 7,
  188. "TCP_WINDOW_CLAMP" : 10}
  189. @retry_on_failure
  190. def test_getprotobyname():
  191. '''
  192. Tests socket.getprotobyname
  193. '''
  194. #IP and CPython
  195. proto_map = {
  196. "icmp": socket.IPPROTO_ICMP,
  197. "ip": socket.IPPROTO_IP,
  198. "tcp": socket.IPPROTO_TCP,
  199. "udp": socket.IPPROTO_UDP,
  200. }
  201. #supported only by IP
  202. if is_cli:
  203. proto_map.update(
  204. {"dstopts": socket.IPPROTO_DSTOPTS,
  205. "none": socket.IPPROTO_NONE,
  206. "raw": socket.IPPROTO_RAW,
  207. "ipv4": socket.IPPROTO_IPV4,
  208. "ipv6": socket.IPPROTO_IPV6,
  209. "esp": socket.IPPROTO_ESP,
  210. "fragment": socket.IPPROTO_FRAGMENT,
  211. "nd": socket.IPPROTO_ND,
  212. "icmpv6": socket.IPPROTO_ICMPV6,
  213. "routing": socket.IPPROTO_ROUTING,
  214. "pup": socket.IPPROTO_PUP, #http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=21918
  215. "ggp": socket.IPPROTO_GGP, #http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=21918
  216. })
  217. for proto_name, good_val in proto_map.iteritems():
  218. temp_val = socket.getprotobyname(proto_name)
  219. AreEqual(temp_val, good_val)
  220. #negative cases
  221. bad_list = ["", "blah", "i"]
  222. for name in bad_list:
  223. AssertError(socket.error, socket.getprotobyname, name)
  224. @retry_on_failure
  225. def test_getaddrinfo():
  226. '''
  227. Tests socket.getaddrinfo
  228. '''
  229. joe = { ("127.0.0.1", 0) : "[(2, 0, 0, '', ('127.0.0.1', 0))]",
  230. ("127.0.0.1", 1) : "[(2, 0, 0, '', ('127.0.0.1', 1))]",
  231. ("127.0.0.1", 0, 0) : "[(2, 0, 0, '', ('127.0.0.1', 0))]",
  232. ("127.0.0.1", 0, 0, 0) : "[(2, 0, 0, '', ('127.0.0.1', 0))]",
  233. ("127.0.0.1", 0, 0, 0, 0) : "[(2, 0, 0, '', ('127.0.0.1', 0))]",
  234. ("127.0.0.1", 0, 0, 0, 0, 0) : "[(2, 0, 0, '', ('127.0.0.1', 0))]",
  235. ("127.0.0.1", 0, 0, 0, 0, 0) : "[(2, 0, 0, '', ('127.0.0.1', 0))]",
  236. ("127.0.0.1", 0, 0, 0, 0, 1) : "[(2, 0, 0, '', ('127.0.0.1', 0))]",
  237. }
  238. tmp = socket.getaddrinfo("127.0.0.1", 0, 0, 0, -100000, 0)
  239. tmp = socket.getaddrinfo("127.0.0.1", 0, 0, 0, 100000, 0)
  240. tmp = socket.getaddrinfo("127.0.0.1", 0, 0, 0, 0, 0)
  241. #just try them as-is
  242. for params,value in joe.iteritems():
  243. addrinfo = socket.getaddrinfo(*params)
  244. AreEqual(repr(addrinfo), value)
  245. #change the address family
  246. for addr_fam in ["AF_INET", "AF_UNSPEC"]:
  247. addrinfo = socket.getaddrinfo("127.0.0.1",
  248. 0,
  249. eval("socket." + addr_fam),
  250. 0,
  251. 0,
  252. 0)
  253. AreEqual(repr(addrinfo), "[(2, 0, 0, '', ('127.0.0.1', 0))]")
  254. #change the socket type
  255. for socktype in ["SOCK_DGRAM", "SOCK_RAW", "SOCK_STREAM"]:
  256. socktype = eval("socket." + socktype)
  257. addrinfo = socket.getaddrinfo("127.0.0.1",
  258. 0,
  259. 0,
  260. socktype,
  261. 0,
  262. 0)
  263. AreEqual(repr(addrinfo), "[(2, " + str(socktype) + ", 0, '', ('127.0.0.1', 0))]")
  264. #change the protocol
  265. for proto in IPPROTO_DICT.keys():#["SOCK_DGRAM", "SOCK_RAW", "SOCK_STREAM"]:
  266. try:
  267. proto = eval("socket." + proto)
  268. except:
  269. print proto
  270. continue
  271. addrinfo = socket.getaddrinfo("127.0.0.1",
  272. 0,
  273. 0,
  274. 0,
  275. proto,
  276. 0)
  277. AreEqual(repr(addrinfo), "[(2, 0, " + str(proto) + ", '', ('127.0.0.1', 0))]")
  278. #negative cases
  279. #TODO - this actually passes on a Windows 7 machine...
  280. #AssertError(socket.gaierror, socket.getaddrinfo, "should never work.dfkdfjkkjdfkkdfjkdjf", 0)
  281. AssertError(socket.gaierror, socket.getaddrinfo, "1", 0)
  282. AssertError(socket.gaierror, socket.getaddrinfo, ".", 0)
  283. AssertError(socket.error, socket.getaddrinfo, "127.0.0.1", 3.14, 0, 0, 0, 0)
  284. AssertError(socket.error, socket.getaddrinfo, "127.0.0.1", 0, -1, 0, 0, 0)
  285. AssertError(socket.error, socket.getaddrinfo, "127.0.0.1", 0, 0, -1, 0, 0)
  286. socket.getaddrinfo("127.0.0.1", 0, 0, 0, 1000000, 0)
  287. socket.getaddrinfo("127.0.0.1", 0, 0, 0, -1000000, 0)
  288. socket.getaddrinfo("127.0.0.1", 0, 0, 0, 0, 0)
  289. @retry_on_failure
  290. def test_getnameinfo():
  291. '''
  292. Tests socket.getnameinfo()
  293. '''
  294. #sanity
  295. socket.getnameinfo(("127.0.0.1", 80), 8)
  296. socket.getnameinfo(("127.0.0.1", 80), 9)
  297. host, service = socket.getnameinfo( ("127.0.0.1", 80), 8)
  298. AreEqual(service, '80')
  299. if is_cli:
  300. AssertError(NotImplementedError, socket.getnameinfo, ("127.0.0.1", 80), 0)
  301. #IP gives a TypeError
  302. #AssertError(SystemError, socket.getnameinfo, ("127.0.0.1"), 8)
  303. #AssertError(SystemError, socket.getnameinfo, (321), 8)
  304. AssertError(TypeError, socket.getnameinfo, ("127.0.0.1"), '0')
  305. AssertError(TypeError, socket.getnameinfo, ("127.0.0.1", 80, 0, 0, 0), 8)
  306. AssertError(socket.gaierror, socket.getnameinfo, ('no such host will ever exist', 80), 8)
  307. @retry_on_failure
  308. def test_gethostbyaddr():
  309. '''
  310. Tests socket.gethostbyaddr
  311. '''
  312. socket.gethostbyaddr("localhost")
  313. socket.gethostbyaddr("127.0.0.1")
  314. if is_cli and not is_net40: #http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=24495
  315. socket.gethostbyaddr("<broadcast>")
  316. @retry_on_failure
  317. def test_gethostbyname():
  318. '''
  319. Tests socket.gethostbyname
  320. '''
  321. #sanity
  322. AreEqual(socket.gethostbyname("localhost"), "127.0.0.1")
  323. AreEqual(socket.gethostbyname("127.0.0.1"), "127.0.0.1")
  324. AreEqual(socket.gethostbyname("<broadcast>"), "255.255.255.255")
  325. #negative
  326. AssertError(socket.gaierror, socket.gethostbyname, "should never work")
  327. @retry_on_failure
  328. def test_gethostbyname_ex():
  329. '''
  330. Tests socket.gethostbyname_ex
  331. '''
  332. #sanity
  333. joe = socket.gethostbyname_ex("localhost")[2]
  334. Assert(joe.count("127.0.0.1")==1)
  335. joe = socket.gethostbyname_ex("127.0.0.1")[2]
  336. Assert(joe.count("127.0.0.1")==1)
  337. #negative
  338. AssertError(socket.gaierror, socket.gethostbyname_ex, "should never work")
  339. @retry_on_failure
  340. def test_getservbyport():
  341. if is_cli:
  342. AssertError(NotImplementedError, socket.getservbyport, 80)
  343. @retry_on_failure
  344. def test_getservbyname():
  345. if is_cli:
  346. AssertError(NotImplementedError, socket.getservbyname, "http")
  347. @retry_on_failure
  348. def test_inet_ntop():
  349. '''
  350. Tests socket.inet_ntop
  351. '''
  352. if not is_cli:
  353. return
  354. #negative
  355. AssertError(socket.error, socket.inet_ntop, socket.AF_INET, "garbage dkfjdkfjdkfj")
  356. @retry_on_failure
  357. def test_inet_pton():
  358. '''
  359. Tests socket.inet_pton
  360. '''
  361. if not is_cli:
  362. return
  363. #sanity
  364. socket.inet_pton(socket.AF_INET, "127.0.0.1")
  365. #negative
  366. AssertError(socket.error, socket.inet_pton, socket.AF_INET, "garbage dkfjdkfjdkfj")
  367. @retry_on_failure
  368. def test_getfqdn():
  369. '''
  370. Tests socket.getfqdn
  371. '''
  372. #TODO
  373. pass
  374. @retry_on_failure
  375. def test_cp12452():
  376. '''
  377. Fully test socket._fileobj compatibility
  378. '''
  379. expected_dir = [
  380. '__module__',
  381. #-- Implementation dependent
  382. #'__slots__',
  383. #-- "_xyz" members probably do not need to be reimplemented in IP...
  384. #'_close', '_get_wbuf_len', '_getclosed', '_rbuf',
  385. #'_rbufsize', '_sock', '_wbuf', '_wbufsize',
  386. '__class__', '__del__', '__delattr__', '__doc__',
  387. '__getattribute__', '__hash__', '__init__', '__iter__',
  388. '__new__', '__reduce__', '__reduce_ex__',
  389. '__repr__', '__setattr__', '__str__',
  390. 'bufsize',
  391. 'close', 'closed', 'default_bufsize', 'fileno', 'flush',
  392. 'mode', 'name', 'next', 'read', 'readline', 'readlines',
  393. 'softspace', 'write', 'writelines']
  394. fileobject_dir = dir(socket._fileobject)
  395. missing = [ x for x in expected_dir if x not in fileobject_dir ]
  396. AreEqual([], missing)
  397. @retry_on_failure
  398. def test_misc():
  399. f = socket.socket().makefile()
  400. f.bufsize = 4096
  401. AreEqual(4096, f.bufsize)
  402. #Dev10 446426
  403. @skip("multiple_execute")
  404. @retry_on_failure
  405. def test_makefile_refcount():
  406. "Ensures that the socket stays open while there's still a file associated"
  407. def echoer(port):
  408. s = socket.socket()
  409. s.bind(('localhost', port))
  410. s.listen(5)
  411. (s2, ignore) = s.accept()
  412. s2.send(s2.recv(10))
  413. port = 50008
  414. thread.start_new_thread(echoer, (port, ))
  415. time.sleep(0)
  416. s = socket.socket()
  417. s.connect(('localhost', port))
  418. f1 = s.makefile('r')
  419. f2 = s.makefile('w')
  420. s.close()
  421. test_msg = 'abc\n'
  422. f2.write(test_msg)
  423. f2.flush()
  424. str = f1.readline()
  425. Assert(str==test_msg)
  426. @retry_on_failure
  427. def test_fileobject_close():
  428. """verify we can construct fileobjects w/ the close kw arg"""
  429. fd = socket._fileobject(None, close=True)
  430. AreEqual(fd.mode, 'rb')
  431. if sys.platform=="win32":
  432. #CodePlex 17894
  433. AreEqual(fd.closed, True)
  434. @disabled("TODO: fails consistently on certain machines")
  435. @retry_on_failure
  436. def test_cp5814():
  437. global HAS_EXITED
  438. global EXIT_CODE
  439. HAS_EXITED = False
  440. import nt
  441. import thread
  442. import time
  443. #Server code
  444. server = """
  445. from time import sleep
  446. import socket
  447. HOST = 'localhost'
  448. PORT = 50007
  449. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  450. s.bind((HOST, PORT))
  451. try:
  452. s.listen(1)
  453. conn, addr = s.accept()
  454. #Whatever we get from the client, send it back.
  455. data = conn.recv(1024)
  456. conn.send(data)
  457. #Verifications
  458. if not addr[0] in [HOST, '127.0.0.1']:
  459. raise Exception('The address, %s, was unexpected' % str(addr))
  460. if data!='stuff':
  461. raise Exception('%s!=stuff' % str(data))
  462. sleep(10)
  463. finally:
  464. conn.close()
  465. """
  466. #Spawn off a thread to startup the server
  467. def server_thread():
  468. global EXIT_CODE
  469. global HAS_EXITED
  470. import nt
  471. serverFile = path_combine(testpath.temporary_dir, "cp5814server.py")
  472. write_to_file(serverFile, server)
  473. EXIT_CODE = nt.system("%s %s" %
  474. (sys.executable, serverFile))
  475. HAS_EXITED = True
  476. try:
  477. nt.remove(serverFile)
  478. except:
  479. pass
  480. thread.start_new_thread(server_thread, ())
  481. #Give the server a chance to startup
  482. time.sleep(5)
  483. #Client
  484. HOST = 'localhost'
  485. PORT = 50007
  486. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  487. s.connect((HOST, PORT))
  488. s.send("stuff")
  489. data, addr = s.recvfrom(1024)
  490. s.close()
  491. #Ensure the server didn't die
  492. for i in xrange(100):
  493. if not HAS_EXITED:
  494. print "*",
  495. time.sleep(1)
  496. else:
  497. AreEqual(EXIT_CODE, 0)
  498. break
  499. Assert(HAS_EXITED)
  500. #Verification
  501. AreEqual(data, "stuff")
  502. if sys.platform=="win32":
  503. AreEqual(addr[0], 0)
  504. @disabled("TODO: fails consistently on certain machines")
  505. @retry_on_failure
  506. def test_cp7451():
  507. global HAS_EXITED
  508. global EXIT_CODE
  509. HAS_EXITED = False
  510. import nt
  511. import thread
  512. import time
  513. #Server code
  514. server = """
  515. from time import sleep
  516. import socket
  517. HOST = 'localhost'
  518. PORT = 50007
  519. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  520. s.bind((HOST, PORT))
  521. try:
  522. s.listen(1)
  523. conn, addr = s.accept()
  524. #Whatever we get from the client, send it back.
  525. data = conn.recv(1024)
  526. conn.send(data)
  527. #Verifications
  528. if not addr[0] in [HOST, '127.0.0.1']:
  529. raise Exception('The address, %s, was unexpected' % str(addr))
  530. if data!='stuff2':
  531. raise Exception('%s!=stuff2' % str(data))
  532. sleep(10)
  533. finally:
  534. conn.close()
  535. """
  536. #Spawn off a thread to startup the server
  537. def server_thread():
  538. global EXIT_CODE
  539. global HAS_EXITED
  540. import nt
  541. serverFile = path_combine(testpath.temporary_dir, "cp7451server.py")
  542. write_to_file(serverFile, server)
  543. EXIT_CODE = nt.system("%s %s" %
  544. (sys.executable, serverFile))
  545. HAS_EXITED = True
  546. try:
  547. nt.remove(serverFile)
  548. except:
  549. pass
  550. thread.start_new_thread(server_thread, ())
  551. #Give the server a chance to startup
  552. time.sleep(5)
  553. #Client
  554. HOST = 'localhost'
  555. PORT = 50007
  556. s = socket.socket()
  557. s.connect((HOST, PORT))
  558. s.send("stuff2")
  559. f = s.makefile()
  560. s.close()
  561. #Ensure the server didn't die
  562. for i in xrange(100):
  563. if not HAS_EXITED:
  564. print "*",
  565. time.sleep(1)
  566. else:
  567. AreEqual(EXIT_CODE, 0)
  568. break
  569. Assert(HAS_EXITED)
  570. #Verification
  571. AreEqual(f.read(6), "stuff2")
  572. #------------------------------------------------------------------------------
  573. run_test(__name__)